首页 > 解决方案 > 将字符串转换为 R 中汇总函数的对象参数

问题描述

m6 数据框 我正在对数据集的列名实现循环。代码如下

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])

当我编写正确代码的单个片段时得到的输出。

我想要的输出

标签: rdplyr

解决方案


推荐阅读