R基于列名长度的摘要

R基于列名长度的摘要,r,dataframe,summary,R,Dataframe,Summary,我有以下问题: 我有一个80列的矩阵,其名称有10/11、21/22、31/32或42/43个字符。名称完全不同,但长度始终适合四组中的一组。现在我想添加四列,如果我得到对应于一个组的所有列的值的总和。这里有一个小例子来说明我的意思 a<-rnorm(1:100) b<-rnorm(1:100) cc<-rnorm(1:100) dd<-rnorm(1:100) eee<-rnorm(1:100) fff<-rnorm(1:100) g<-data.fr

我有以下问题: 我有一个80列的矩阵,其名称有10/11、21/22、31/32或42/43个字符。名称完全不同,但长度始终适合四组中的一组。现在我想添加四列,如果我得到对应于一个组的所有列的值的总和。这里有一个小例子来说明我的意思

a<-rnorm(1:100)
b<-rnorm(1:100)
cc<-rnorm(1:100)
dd<-rnorm(1:100)
eee<-rnorm(1:100)
fff<-rnorm(1:100)
g<-data.frame(a,b,cc,dd,eee,fff)
g$group1<-"sum of all columns of with headers of length 1 (in this case a+b)"
g$group2<-"sum of all columns of with headers of length 2 (in this case cc+dd)"
g$group3<-"sum of all columns of with headers of length 3 (in this case eee+fff)"
a您想要这个:

tmp <- nchar(names(g))
chargroups <- split(1:dim(g)[2], tmp)
# `chargroups` is a list of groups of columns with same number of letters in name
sapply(chargroups, function(x) {
    if(length(x)>1) # rowSums can only accept 2+-dimensional object
        rowSums(g[,x])
    else
        g[,x]
})
# `x` is, for each number of letters, a vector of column indices of `g`
另一种方法

set.seed(123)
a <- rnorm(1:100)
b <- rnorm(1:100)
cc <- rnorm(1:100)
dd <- rnorm(1:100)
eee <- rnorm(1:100)
fff <- rnorm(1:100)
g <- data.frame(a,b,cc,dd,eee,fff)

for ( i in 1:3 )
    eval(parse(text = sprintf("g$group%s <- rowSums(g[nchar(names(g)) == %s])", i, i)))

## 'data.frame':    100 obs. of  9 variables:
##  $ a     : num  -0.5605 -0.2302 1.5587 0.0705 0.1293 ...
##  $ b     : num  -0.71 0.257 -0.247 -0.348 -0.952 ...
##  $ cc    : num  2.199 1.312 -0.265 0.543 -0.414 ...
##  $ dd    : num  -0.715 -0.753 -0.939 -1.053 -0.437 ...
##  $ eee   : num  -0.0736 -1.1687 -0.6347 -0.0288 0.6707 ...
##  $ fff   : num  -0.602 -0.994 1.027 0.751 -1.509 ...
##  $ group1: num  -1.2709 0.0267 1.312 -0.277 -0.8223 ...
##  $ group2: num  1.484 0.56 -1.204 -0.509 -0.851 ...
##  $ group3: num  -0.675 -2.162 0.392 0.722 -0.838 ...
set.seed(123)

a谢谢托马斯,他做了这项工作。如果我想将每一行的值分别附加到数据帧g,我需要更改什么?请查看
rowSums
,而不是
sum
。这将为每一组列返回一个行和向量,然后您可以轻松地将其
cbind
转换为原始df.works,如示例所示。如果我添加另一个长度为4的列(例如“hhh”),它会给我以下错误:“基中的错误::行和(x,na.rm=na.rm,dims=dims,…):“x”必须是至少两个维度的数组“如何解释?在手册中提供的帮助下,我无法做到这一点。命令中的“x”代表什么?它是在哪里生成的?抱歉,如果这个问题看起来很愚蠢,但我不完全理解你的代码为什么工作。但是,对于我提供的示例,它确实存在。@user2386786检查编辑
rowSums
不接受向量作为其输入,只接受2+维数组,因此,如果组中有多个列,则该代码现在将为您提供行和;如果只有一列,则只提供列的值。
set.seed(123)
a <- rnorm(1:100)
b <- rnorm(1:100)
cc <- rnorm(1:100)
dd <- rnorm(1:100)
eee <- rnorm(1:100)
fff <- rnorm(1:100)
g <- data.frame(a,b,cc,dd,eee,fff)

for ( i in 1:3 )
    eval(parse(text = sprintf("g$group%s <- rowSums(g[nchar(names(g)) == %s])", i, i)))

## 'data.frame':    100 obs. of  9 variables:
##  $ a     : num  -0.5605 -0.2302 1.5587 0.0705 0.1293 ...
##  $ b     : num  -0.71 0.257 -0.247 -0.348 -0.952 ...
##  $ cc    : num  2.199 1.312 -0.265 0.543 -0.414 ...
##  $ dd    : num  -0.715 -0.753 -0.939 -1.053 -0.437 ...
##  $ eee   : num  -0.0736 -1.1687 -0.6347 -0.0288 0.6707 ...
##  $ fff   : num  -0.602 -0.994 1.027 0.751 -1.509 ...
##  $ group1: num  -1.2709 0.0267 1.312 -0.277 -0.8223 ...
##  $ group2: num  1.484 0.56 -1.204 -0.509 -0.851 ...
##  $ group3: num  -0.675 -2.162 0.392 0.722 -0.838 ...