如何解决将串联字符串作为 dplyr::summarise 中的列名传递 数据
我正在尝试使用连接字符串作为列名迭代地执行 dplyr 汇总
Category=c("a","a","b","c","c")
A1=c(1,2,3,4,1,2)
A2=c(10,11,12,13,14,15,16,17)
tt=cbind(Category,A1,A2)
tdat=data.frame(tt)
colnames(tdat)=c("Category","M1","M2")
ll=matrix(1:2,nrow=2)
for(i in 1:nrow(ll)) {
Aone=tdat %>% group_by(Category) %>%
summarize(Msum=sum(paste("M",i,sep="")))
}
我最终出现以下错误
x invalid 'type' (character) of argument
ℹ Input Msum is sum(paste("M",sep = "")).
ℹ The error occurred in group 1: Category = "A".
Run rlang::last_error() to see where the error occurred.```
The goal is to iteratively get arithmentic functions within summarize function in dplyr. But this concatenated string is not recognized as column name.
解决方法
如果我们想传递一个字符串作为列名,则转换为 sym
bol 并计算 (!!
)
library(dplyr)
Aone <- vector('list',nrow(ll))
for(i in seq_len(nrow(ll))) {
Aone[[i]] <- tdat %>%
group_by(Category) %>%
summarize(Msum = sum(!! rlang::sym(paste("M",i,sep=""))))
}
或者假设列名是'M-1'、'M-2'等,它应该也能工作
Aone <- vector('list',2)
for(i in seq_along(Aone)) {
Aone[[i]] <- tdat %>%
group_by(Category) %>%
summarise(Msum = sum(!! rlang::sym(paste("M-",sep=""))),.groups = 'drop')
}
注意:原帖中的 ll
并不清楚。在这里,我们创建一个 list
,其中 length
等于 'M-' 列的数量,并通过循环遍历该 {{1} 的序列将输出分配回 list
元素}
数据
list
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。