根据R中的匹配条件组合行中的值

Mon*_*eck 8 r dataframe

我有一个关于在R中聚合值的简单问题

假设我有一个数据帧:

DF <- data.frame(col1=c("Type 1", "Type 1B", "Type 2"), col2=c(1, 2, 3))  
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

     col1 col2
1  Type 1    1
2 Type 1B    2
3  Type 2    3
Run Code Online (Sandbox Code Playgroud)

我注意到,我有Type 1Type 1B数据,所以我想结合Type 1BType 1.

所以我决定使用dplyr:

filter(DF, col1=='Type 1' | col1=='Type 1B') %>%
  summarise(n = sum(col2))
Run Code Online (Sandbox Code Playgroud)

但现在我需要坚持下去:

DF2 <- data.frame('Type 1', filter(DF, col1=='Type 1' | col1=='Type 1B') %>%
  summarise(n = sum(col2)))
Run Code Online (Sandbox Code Playgroud)

我想我想把cbind这个新的DF2重新回到原来的DF,但这意味着我必须将列名设置为一致:

names(DF2) <- c('col1', 'col2')
Run Code Online (Sandbox Code Playgroud)

好的,现在我可以回复:

rbind(DF2, DF[3,])
Run Code Online (Sandbox Code Playgroud)

结果?有效....

   col1 col2
1 Type 1    3
3 Type 2    3
Run Code Online (Sandbox Code Playgroud)

......但是唉!太糟糕了!必须有一种更好的方法来简单地组合价值观.

tal*_*lat 5

这是一种可能的 dplyr 方法:

library(dplyr)
DF %>%
  group_by(col1 = sub("(.*\\d+).*$", "\\1", col1)) %>%
  summarise(col2 = sum(col2))
#Source: local data frame [2 x 2]
#
#    col1 col2
#1 Type 1    3
#2 Type 2    3
Run Code Online (Sandbox Code Playgroud)


Col*_*vel 2

你可以试试:

library(data.table)

setDT(transform(DF, col1=gsub("(.*)[A-Z]+$","\\1",DF$col1)))[,list(col2=sum(col2)),col1]

#      col1 col2
# 1: Type 1    3
# 2: Type 2    3
Run Code Online (Sandbox Code Playgroud)

或者更直接:

setDT(DF)[, .(col2 = sum(col2)), by = .(col1 = sub("[[:alpha:]]+$", "", col1))]
Run Code Online (Sandbox Code Playgroud)