for(i in 14:20){
col_name <- colnames(m6[i]) #m6 is the name of the dataframe see the image for reference
mean_group1 = paste(paste(col_name, "group1", sep="_"),"= mean(",col_name, "[group == '1'])",sep="") #here [group == '1'], group is the name of a column in m6 dataframe
mean_group2 = paste(paste(col_name, "group2", sep="_")," = mean(",col_name, "[group == '2'])", sep="")
mean_group3 = paste(paste(col_name, "group3", sep="_")," = mean(",col_name, "[group == '3'])",sep="")
formula_f_2 <- as.formula(paste(mean_group1, mean_group2,mean_group3, sep = ""))
pl <- m6 %>%
group_by(CHILD_ID) %>% #CHILD_ID is the name of a column which is being grouped
summarize(formula_f_2)
}
此代码的问题是给我一个错误。我认为这可能是由于我将字符串作为参数传递给了Summarize函数,该函数无法理解。当我在没有循环的情况下实现代码时(通过为每种情况编写段),代码可以正常运行。起作用的代码如下:
#This is the code which I wrote for when i is 20 i.e. for colnames(m6[20])
pl <- m6 %>% group_by(CHILD_ID) %>% summarize(mean_group1 = mean(A_SD_zscore[group == '1']),
mean_group2 = mean(A_SD_zscore[group == '2']),mean_group3 = mean(A_SD_zscore[group == '3']))
#
#Here m6 is a dataset on which I am applying group and summarize function
#Here A_SD_zscore is colname(m6[20])
当我编写正确的代码时得到的输出。