Finding numbers in a range of text data

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.

Please help!!!!

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


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

#> 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
Df <- data.frame(Score = c("Grade 2", "Mid 3", "Late 4", "Grade 2"))
#>     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"))
#>     Score GradeNumber Quality
#> 1 Grade 2           2     Bad
#> 2   Mid 3           3    Good
#> 3  Late 4           4    Good
#> 4 Grade 2           2     Bad

Created on 2021-09-10 by the reprex package (v0.3.0)

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?

Thank you but I figured it out!

Would it be possible for you to post a solution here ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.