简体   繁体   中英

Assigning column value based on values in three other columns

I started learning R last week and have a dataset of test scores for students K-12. There is a column for grade (k-12), Discipline (Math, Reading, Science), and a test score. I need to create a column that links the test score with a proficiency level. So if a student scores between 0-147, they are assigned a "limited" proficiency level, 148-158 "standard" and so on.

Dataset looks like this:

enter image description here

And so on. I want it to look like this:

enter image description here

The issue is, the cut points for the different proficiency levels change by grade and by subject. I am trying to avoid doing a huge ifelse loop in R.

For example, a 141 test score for kindergarden math would be a limited proficiency level, but the same score in reading would be "standard" level. A score of 141 in fourth grade math might be "proficient"

I tried assigning vectors for the cutpoints for each subject and grade combination like this so I could use the findInterval functions

K_math <- c(0, 147, 151, 158, 167, 350)
K_reading <- c(0, 135, 147, 159, 169, 350)
levels <- c("Limited", "Standard", "Proficient", "Accelerated", "Advanced")

Then

mutate(test_scores$_df$proficiency_level <- case_when(Grade == "K" & Discipline == "Mathematics" & findInterval(test_scores_df$RawTestScore, K_math, left.open = TRUE) ~ levels))

Please help

We can use cut

library(dplyr)
test_score %>%
      mutate(proficiency_level = cut(score, breaks = K_math, labels = levels))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM