r - 将字符串转换为 R 中汇总函数的对象参数
问题描述
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])
当我编写正确代码的单个片段时得到的输出。
解决方案
推荐阅读
- postgresql - 如何从 Postgres 的时间戳中自动提取或索引日期?
- ruby - Capybara:如何更改窗口大小?
- c++ - 将文本文件解析为树状数据结构
- javascript - groupBy 和 sortBy 原生 ES6 中的对象数组
- python - 遍历数组时出现ValueError
- html - 为 PostCSS 设置配置文件
- google-apps-script - Apps 脚本:如何以编程方式调整带有包裹单元格的 Google 表格行的大小,使其小于包裹的文本
- linux - How to Get Fully Functional PowerShell running on Linux?
- amazon-web-services - Golang AWS API Gateway 无效字符“e”寻找值的开头
- javascript - bind 如何与 new 和 instanceof 一起工作?