scunha
1
Let's say I have the following dataset where I asked people to tell me what their staff role was and they were able to select multiple questions.
How can I tell R to calculate the mean score for the string 'Teaching'?
> 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)
> 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
FJCC
2
Here is one method that uses str_detect() from the stringr package. Notice that I manually changed the score column from characters to numbers.
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
library(tidyverse)
df |> filter(str_detect(Staff,"Teaching")) |>
summarize(Avg=mean(score))
#> Avg
#> 1 2.5
Created on 2022-10-24 with reprex v2.0.2
scunha
3
Thank you, this was really helpful!
system
Closed
4
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.