我们可以在R 中使用dplyr。我们按前两列分组,mutate 第 3 列 ('V3') 作为该列的 sum,并使用 summarise_each 得到列 3:6 的 mean。
library(dplyr)
res <- df1 %>%
group_by(V1, V2) %>%
mutate(V3=sum(V3)) %>%
summarise_each(funs(round(mean(.),1)), V3:V6)
as.data.frame(res)
# V1 V2 V3 V4 V5 V6
#1 0 77 1 2.0 3.0 5.0
#2 0 78 3 3.0 4.5 3.0
#3 3 79 7 4.7 3.3 1.3
数据
df1 <- structure(list(V1 = c(0L, 0L, 0L, 3L, 3L, 3L), V2 = c(77L, 78L,
78L, 79L, 79L, 79L), V3 = c(1L, 2L, 1L, 0L, 6L, 1L), V4 = c(2L,
4L, 2L, 4L, 8L, 2L), V5 = c(3L, 6L, 3L, 5L, 2L, 3L), V6 = c(5L,
1L, 5L, 2L, 1L, 1L)), .Names = c("V1", "V2", "V3", "V4", "V5",
"V6"), class = "data.frame", row.names = c(NA, -6L))