我在Excel中有一些行名称及其各自的出版物。
Name Pub
AAAA MNWP
AAAA TIWD
BBBB CGWK
BBBB MNWP
CCCC EXWE
CCCC EXWE
CCCC HXWT
CCCC MDWE
CCCC MDWE
CCCC MNWP
CCCC MNWP
CCCC NDWK
DDDD MNWP
EEEE EXWE
EEEE TIWE
基本上我希望列表具有以下形式:
AAAA: MNWP, TIWD
BBBB: CGWK, MNWP
CCCC: EXWE, HXWT, MDWE, MNWP, NDWK, TIWE
DDDD: MNWP
EEEE: EXWE, TIWE
有没有办法以上面的格式合并此列表? Excel
或R
(最好是Excel)。
答案 0 :(得分:2)
使用data.table
library(data.table)
setDT(dat)[, list(Group=paste(Pub, collapse=", ")), by=Name][,paste(Name, Group, sep=": ")]
#[1] "AAAA: MNWP, TIWD"
#[2] "BBBB: CGWK, MNWP"
#[3] "CCCC: EXWE, EXWE, HXWT, MDWE, MDWE, MNWP, MNWP, NDWK"
#[4] "DDDD: MNWP"
#[5] "EEEE: EXWE, TIWE"
答案 1 :(得分:1)
一种方法是
x <- read.table(text = "Name Pub
AAAA MNWP
AAAA TIWD
BBBB CGWK
BBBB MNWP
CCCC EXWE
CCCC EXWE
CCCC HXWT
CCCC MDWE
CCCC MDWE
CCCC MNWP
CCCC MNWP
CCCC NDWK
DDDD MNWP
EEEE EXWE
EEEE TIWE ", header = TRUE)
aggregate(Pub ~ Name, data = x, FUN = paste)
Name Pub
1 AAAA MNWP, TIWD
2 BBBB CGWK, MNWP
3 CCCC EXWE, EXWE, HXWT, MDWE, MDWE, MNWP, MNWP, NDWK
4 DDDD MNWP
5 EEEE EXWE, TIWE
答案 2 :(得分:1)
R中的另一个选项:
library(dplyr)
df %>%
group_by(Name) %>%
summarise(Pub = paste(Pub, collapse =", "))
#Source: local data frame [5 x 2]
#
# Name Pub
#1 AAAA MNWP, TIWD
#2 BBBB CGWK, MNWP
#3 CCCC EXWE, EXWE, HXWT, MDWE, MDWE, MNWP, MNWP, NDWK
#4 DDDD MNWP
#5 EEEE EXWE, TIWE