如何在R中:添加行

时间:2015-02-05 00:18:11

标签: r

我有一个名为df的数据框,我需要将某些行组合在一起。输入数据我犯了一个错误,有些人没有接受治疗就出来了。我知道下面G13B实际上是G13S。我的数据很快就会在下面组织,我想在一起添加非常具体的行。

实施例

Individual v1   v2  v3 Treatment
G13B       0    4   6     NA
G13S       1    2   1   Control
G34B       0    4   6     NA
G34S       1    2   1   Control

通缉,我将G13B行添加到G13S行。

Individual v1   v2  v3 Treatment
G13S       1    6   7   Control
G34S       1    6   7   Control

我试图做Rbind,但它只是添加了它上面的列。

1 个答案:

答案 0 :(得分:1)

如果要根据个人ID的前三个字符折叠行,可以使用dplyr执行某些操作。首先,您的样本数据

dd<-structure(list(Individual = structure(1:4, .Label = c("G13B", 
"G13S", "G34B", "G34S"), class = "factor"), v1 = c(0L, 1L, 0L, 
1L), v2 = c(4L, 2L, 4L, 2L), v3 = c(6L, 1L, 6L, 1L), Treatment = structure(c(NA, 
1L, NA, 1L), .Label = "Control", class = "factor")), .Names = c("Individual", 
"v1", "v2", "v3", "Treatment"), class = "data.frame", row.names = c(NA, 
-4L))

然后你可以做

library(dplyr)

dd %>% group_by(IND=substr(Individual,1,3)) %>% 
    summarize(
       Individual=max(as.character(Individual), na.rm=T), 
       v1=sum(v1), 
       v2=sum(v2), 
       v3=sum(v3), 
       Treatment=min(as.character(Treatment), na.rm=T)) %>% 
    ungroup() %>% select(-IND)

返回

  Individual v1 v2 v3 Treatment
1       G13S  1  6  7   Control
2       G34S  1  6  7   Control