I want to be able to categorize values that are greater than or equal to one value as "Good" and those not meeting the condition are coded as "Bad".
For example, I have a list of scores ranging from grade level (Grade 1, Mid 1, Grade 3, Late 4, and so forth) and I want to categorize scores based on grade level. If a score is at or above the grade level, they would be grouped as "Good" (i.e., grade level is 3 but score is Grade 4, this would be "good", on the other hand, if the grade level is 3 but the score is Grade 2, it would be "bad").
I want to write a function so that every value of the grade number (ex: 2) or higher (ex: >2) found within a column of data is returned as "good" in a new column.
Here is a quick example that might help you. I had to guess at the form of the scores and their range because you did not supply any data. If you are still stuck, please post some data. You can use the dput function to do that. Run
dput(head(DF))
where you replace DF with the actual name of your data frame. Post the output of that between lines consisting of three back ticks.
```
Your output
```
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(stringr)
Df <- data.frame(Score = c("Grade 2", "Mid 3", "Late 4", "Grade 2"))
Df
#> Score
#> 1 Grade 2
#> 2 Mid 3
#> 3 Late 4
#> 4 Grade 2
Df <- Df %>% mutate(GradeNumber = str_extract(Score, "\\d"),
Quality = ifelse(GradeNumber >= "3", "Good", "Bad"))
Df
#> Score GradeNumber Quality
#> 1 Grade 2 2 Bad
#> 2 Mid 3 3 Good
#> 3 Late 4 4 Good
#> 4 Grade 2 2 Bad
Thanks but it's little more complex than that. A score is "Good" if it is Mid Grade Level or a higher Grade Level. A score is "Bad if it is Early Grade Level or in a lower grade. For instance, the grade level could be 3 and the score could be Early 3 (which is "bad"), Mid 3 (which is "good), Grade 4 (which is "good"). The complexity here is that I need to be able to do this for any given grade level. Does that make sense?