如何将多个列与grep组合并对r中的值求和

如何将多个列与grep组合并对r中的值求和,r,R,我在r中有以下数据帧 Engine General Ladder.winch engine.phe subm.gear.box aux.engine pipeline.maintain pipeline pipe.line engine.mpd 1 12 22 2 4 2 4 5

我在r中有以下数据帧

Engine   General   Ladder.winch   engine.phe   subm.gear.box   aux.engine   pipeline.maintain    pipeline    pipe.line    engine.mpd
 1        12        22             2            4               2             4                    5            6             7
等等,超过10000行

现在,我想合并列并添加值,以将列减少到更广泛的类别中。e、 g
Engine,Engine.phe,aux.Engine,Engine.mpd
应组合到
Engine
类别中,并添加所有值。同样地,
pipeline.maintain、pipeline、pipeline.line
将组合到
pipeline
中,其余列将添加到
General
类别下

所需的数据帧是

 Engine      Pipeline       General
   12          15             38

我怎样才能在r中做到这一点?

您可以通过多种方式做到这一点,这是一种更直接的方法

# Example data.frame
dtf <- structure(list(Engine = c(1, 0, 1), 
   General = c(12, 3, 15), Ladder.winch = c(22, 28, 26), 
    engine.phe = c(2, 1, 0), subm.gear.box = c(4, 4, 10), 
    aux.engine = c(2, 3, 1), pipeline.maintain = c(4, 5, 1), 
    pipeline = c(5, 5, 2), pipe.line = c(6, 8, 2), engine.mpd = c(7, 8, 19)),
    .Names = c("Engine", "General", "Ladder.winch", "engine.phe", 
      "subm.gear.box", "aux.engine", "pipeline.maintain", 
      "pipeline", "pipe.line", "engine.mpd"), 
    row.names = c(NA, -3L), class = "data.frame")

with(dtf, data.frame(Engine=Engine+engine.phe+aux.engine+engine.mpd,
                   Pipeline=pipeline.maintain+pipeline+pipe.line,
                    General=General+Ladder.winch+subm.gear.box))

#   Engine Pipeline General
# 1     12       15      38
# 2     12       18      35
# 3     21        5      51

# a more generalized and 'greppy' solution
cnames <- tolower(colnames(dtf))
data.frame(Engine=rowSums(dtf[, grep("eng", cnames)]),
         Pipeline=rowSums(dtf[, grep("pip", cnames)]),
          General=rowSums(dtf[, !grepl("eng|pip", cnames)]))
#示例data.frame

dtf您可以通过多种方式完成,这是一种更直接的方法

# Example data.frame
dtf <- structure(list(Engine = c(1, 0, 1), 
   General = c(12, 3, 15), Ladder.winch = c(22, 28, 26), 
    engine.phe = c(2, 1, 0), subm.gear.box = c(4, 4, 10), 
    aux.engine = c(2, 3, 1), pipeline.maintain = c(4, 5, 1), 
    pipeline = c(5, 5, 2), pipe.line = c(6, 8, 2), engine.mpd = c(7, 8, 19)),
    .Names = c("Engine", "General", "Ladder.winch", "engine.phe", 
      "subm.gear.box", "aux.engine", "pipeline.maintain", 
      "pipeline", "pipe.line", "engine.mpd"), 
    row.names = c(NA, -3L), class = "data.frame")

with(dtf, data.frame(Engine=Engine+engine.phe+aux.engine+engine.mpd,
                   Pipeline=pipeline.maintain+pipeline+pipe.line,
                    General=General+Ladder.winch+subm.gear.box))

#   Engine Pipeline General
# 1     12       15      38
# 2     12       18      35
# 3     21        5      51

# a more generalized and 'greppy' solution
cnames <- tolower(colnames(dtf))
data.frame(Engine=rowSums(dtf[, grep("eng", cnames)]),
         Pipeline=rowSums(dtf[, grep("pip", cnames)]),
          General=rowSums(dtf[, !grepl("eng|pip", cnames)]))
#示例data.frame

dtf最好以长格式存储数据。因此,我建议如下处理您的问题:

1-以长格式获取数据

library(reshape2)
dfl <- melt(df)
现在,您可以使用
aggregate
获得最终结果:

> aggregate(value ~ newcat, dfl, sum)
    newcat value
1   engine    12
2  general    38
3 pipeline    15

最好以长格式存储数据。因此,我建议如下处理您的问题:

1-以长格式获取数据

library(reshape2)
dfl <- melt(df)
现在,您可以使用
aggregate
获得最终结果:

> aggregate(value ~ newcat, dfl, sum)
    newcat value
1   engine    12
2  general    38
3 pipeline    15

这里有一个选项,从列的
名称中提取相关单词,并使用
tapply
获取
总和。
str\u extract\u all
返回一个
列表('lst')。将长度为零的元素替换为“常规”,然后使用分组函数,即
tapply
unlist
数据集,并使用分组变量,即复制的'lst'和'df1'的
,获得

library(stringr)
lst <- str_extract_all(toupper(sub("(pipe)\\.", "\\1", names(df1))),
          "ENGINE|PIPELINE|GENERAL")
lst[lengths(lst)==0] <- "GENERAL"
t(tapply(unlist(df1), list(unlist(lst)[col(df1)], row(df1)), FUN = sum))
#   ENGINE  GENERAL PIPELINE 
#1      12       38       15 
库(stringr)

lst这里有一个选项,它从列的
名称中提取相关单词,并使用
tapply
获得
总和。
str\u extract\u all
返回一个
列表('lst')。将长度为零的元素替换为“常规”,然后使用分组函数,即
tapply
unlist
数据集,并使用分组变量,即复制的'lst'和'df1'的
,获得

library(stringr)
lst <- str_extract_all(toupper(sub("(pipe)\\.", "\\1", names(df1))),
          "ENGINE|PIPELINE|GENERAL")
lst[lengths(lst)==0] <- "GENERAL"
t(tapply(unlist(df1), list(unlist(lst)[col(df1)], row(df1)), FUN = sum))
#   ENGINE  GENERAL PIPELINE 
#1      12       38       15 
库(stringr)
lst
df
是来自


df
是我拥有的
1100
这类列中的数据。我需要更通用的方法,比如
grep
我们可以这样做吗?我希望这样更好。当然,搜索词以及如何定义
general
取决于您的需要。我有
1100
这样的栏目。我需要更通用的方法,比如
grep
我们可以这样做吗?我希望这样更好。当然,搜索词和您如何定义
general
取决于您的需要。当我运行上述代码时,它在tapply(unlist(df[,c(71:105)]、unlist(lst),FUN=sum中给出了一个错误
错误:参数必须具有相同的长度
我还有其他文本列以及这些列。@Neil the
lst
df[,71:105]
具有不同的长度。也许你需要
names(df1)[71:105]
在代码的第一行中我在
tapply
中传递以下参数,
cnames@Neil在
cnames
中你有71:105,之后你使用的是
71:104
长度是否不同?抱歉,这是一个输入错误
cnames是71:105
当我运行上述代码时,它在tapply(unlist(df[,c(71:105)])、unlist(lst),FUN=sum中给了我一个错误
错误:参数必须具有相同的长度
我还有其他文本列与此列一起。@Neil
lst
df[,71:105]
的长度不同。也许你需要
names(df1)[71:105]
在代码的第一行中我在
tapply
中传递以下参数,
cnames@Neil在
cnames
中你有71:105,之后你使用的是
71:104
长度是否不同?抱歉,这是一个输入错误<代码>cnames是71:105
非常感谢您的回答。荣誉非常感谢你的回答。荣誉