I have this dataset
id = c(1,1,1,2,2,3)
v1 = c(3,4,5,2,4,5)
v2 = c(3,1,2,1,4,5)
v3 = c(2,1,2,3,3,4)
mydata <- data.frame(id ,v1, v2, v3)
> mydata
  id v1 v2 v3
1  1  3  3  2
2  1  4  1  1
3  1  5  2  2
4  2  2  1  3
5  2  4  4  3
6  3  5  5  4
and grouped data by id
groupdata <- group_by(mydata, id)
using summarize function can get a specific column mean value by id:
summarize(groupdata, mean = mean(v1))
# A tibble: 3 × 2
     id  mean
  <dbl> <dbl>
1     1     4
2     2     3
3     3     5
what i am tring to do is loop over each column and summarize them
colnames <- names(mydata)
for(i in colnames){
  assign(paste(i,"mean", sep = "_"), summarize(groupdata, mean = mean(i))) 
}
but i got this
> v1_mean
# A tibble: 3 × 2
     id  mean
  <dbl> <lgl>
1     1    NA
2     2    NA
3     3    NA
I found that you can't pass column names into summarize function as the parameter, is there any suggestions to improve the loop function?
 
     
     
    