I have the following data frame
ID <- c(1,1,2,3,4,5,6)
Value1 <- c(20,50,30,10,15,10,NA)
Value2 <- c(40,33,84,NA,20,1,NA)
Value3 <- c(60,40,60,10,25,NA,NA)
Grade1 <- c(20,50,30,10,15,10,NA)
Grade2 <- c(40,33,84,NA,20,1,NA)
DF <- data.frame(ID,Value1,Value2,Value3,Grade1,Grade2)
ID Value1 Value2 Value3 Grade1 Grade2
1 1 20 40 60 20 40
2 1 50 33 40 50 33
3 2 30 84 60 30 84
4 3 10 NA 10 10 NA
5 4 15 20 25 15 20
6 5 10 1 NA 10 1
7 6 NA NA NA NA NA
I would like to group by ID, select columns with names contain the string ("Value"), and get the mean of these columns with NA not included.
Here is an example of the desired output
ID mean(Value)
1 41
2 58
3 10
....
In my attempt to solve this challenge, I wrote the following code
Library(tidyverse)
DF %>% group_by (ID) %>% select(contains("Value")) %>% summarise(mean(.,na.rm = TRUE))
The code groups the data by IDs, select columns with column name containing ("Value"), and attempts to summarise the selected columns by using the mean function. When I run my code, I get the following output
> DF %>% group_by (ID) %>% select(contains("Value")) %>% summarise(mean(.))
Adding missing grouping variables: `ID`
# A tibble: 6 x 2
ID `mean(.)`
<dbl> <dbl>
1 1 NA
2 2 NA
3 3 NA
4 4 NA
5 5 NA
6 6 NA
I would appreciate your help in this manner.