How can I recode values according to strings

Let's say I have the following dataset:

df = data.frame(Staff=c('Teaching','Research','Research,Teaching','Administration', 'Faculty Management', 'Administration, Faculty Management'), score=c('2','1','3','2','1', '2'), length=c(10, 20, 30, 5, 10, 6))
> View(df)
                              Staff score length
1                           Teaching     2     10
2                           Research     1     20
3                  Research,Teaching     3     30
4                     Administration     2      5
5                 Faculty Management     1     10
6 Administration, Faculty Management     2      6
> 

I am trying to perform an ANOVA to see if there are differences between staff roles' scores.
How can I recode the values so that if a value contains the string "Teaching" it can be recoded as just Teaching?

Or maybe a better question: How do you deal with multiple-answer questions in R?

If the categories are always separated with commas, you can duplicate the rows with the separate_rows() function. Whether it is reasonable to assign all of the score to each category is another question.

library(tidyr)
df = data.frame(Staff=c('Teaching','Research','Research,Teaching','Administration', 'Faculty Management', 'Administration, Faculty Management'), 
                score=c('2','1','3','2','1', '2'), 
                length=c(10, 20, 30, 5, 10, 6))
df
#>                                Staff score length
#> 1                           Teaching     2     10
#> 2                           Research     1     20
#> 3                  Research,Teaching     3     30
#> 4                     Administration     2      5
#> 5                 Faculty Management     1     10
#> 6 Administration, Faculty Management     2      6
df_New <- separate_rows(df,Staff, sep=",")
df_New
#> # A tibble: 8 × 3
#>   Staff                 score length
#>   <chr>                 <chr>  <dbl>
#> 1 "Teaching"            2         10
#> 2 "Research"            1         20
#> 3 "Research"            3         30
#> 4 "Teaching"            3         30
#> 5 "Administration"      2          5
#> 6 "Faculty Management"  1         10
#> 7 "Administration"      2          6
#> 8 " Faculty Management" 2          6

Created on 2022-10-26 with reprex v2.0.2

This topic was automatically closed 42 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.