合并R中的两行

时间:2018-12-07 21:29:51

标签: r aggregate

这是我的数据片段

for(i in 1:length(data$colB)){
  if(is.na(data$colB[i])){
  data$newColumn <- data$colA}
  else{
    data$newColumn <- data$colB
  }
}

我正在尝试合并Arron Afflalo行,但同时添加第1-3列,但取第4-5列的平均值。

所以最终数据输出看起来像

Aaron Brook   450 36 25 .124 .530
Arron Afflalo 980 25 40 .128 .597
Arron Afflalo 236 25 94 .223 .621

有没有快速的方法可以做到这一点。如果没有,是否有办法删除所有重复的行?

2 个答案:

答案 0 :(得分:1)

使用dplyr:

library(dplyr)
df <- data.frame(name = c("Aaron Brook", "Arron Afflalo", "Arron Afflalo"), 
                 x = c(450, 980, 236), y = c(36, 25, 25), 
                 a = c(.124, .128, .223), b = c(.530, .597, .621))

df %>% 
   filter(name == "Arron Afflalo") %>%
   group_by(name) %>%
   summarize(sum_x = sum(x), sum_y = sum(y), mean_a = mean(a), mean_b = mean(b))

如果您不知道各列的名称以及它们的顺序,则也可以将最后一行替换为:

   summarize(sum_x = sum(.[[2]]), sum_y = sum(.[[3]]), mean_a = mean(.[[4]]), mean_b = mean(.[[5]]))

答案 1 :(得分:1)

如果需要所有姓名和姓氏的摘要,请使用tidyverse

df %>%
 group_by(V1, V2) %>%
 mutate_at(3:5, funs(sum(.))) %>%
 mutate_at(6:7, funs(mean(.))) %>%
 summarise_all(funs(first(.)))

  V1    V2         V3    V4    V5    V6    V7
  <fct> <fct>   <int> <int> <int> <dbl> <dbl>
1 Aaron Brook     450    36    25 0.124 0.530
2 Arron Afflalo  1216    50   134 0.176 0.609

或者如果您只想为该特定名称提供摘要:

df %>%
 filter(V1 == "Arron" & V2 == "Afflalo") %>%
 mutate_at(3:5, funs(sum(.))) %>%
 mutate_at(6:7, funs(mean(.))) %>%
 summarise_all(funs(first(.)))

     V1      V2   V3 V4  V5     V6    V7
1 Arron Afflalo 1216 50 134 0.1755 0.609

数据:

df <- read.table(text = "Aaron Brook   450 36 25 .124 .530
Arron Afflalo 980 25 40 .128 .597
Arron Afflalo 236 25 94 .223 .621")