我想按照首先选出的日期创建字符串,如下所示。
DF:
Name Party FirstElected
Bob Liberal 1985
Joe Republican 1985
Sarah Green 1980
Bill Libertarian 1980
Tom Conservative 1987
目标:
Year PeopleElected
1985 "Bob (Liberal); Joe (Republican)"
1980 "Sarah (Green); Bill (Libertarian)"
1987 "Tom (Conservative)"
我认为paste
和apply/aggregate
的某种组合可以做到这一点......但到目前为止我还没有多少运气。
答案 0 :(得分:3)
我们可以使用paste/sprintf
创建按' FirstElected'分组的格式。我们转换了' data.frame'到' data.table' (setDT(df1)
),按' FirstElected'分组,我们将' Party'用括号括起来,用“' Name'使用sprintf
,然后使用paste
和collapse='; '
创建一个字符串。
library(data.table)
setDT(df1)[,list(PeopleElected=paste(sprintf('%s (%s)',
Name, Party), collapse="; ")) , by = FirstElected]
# FirstElected PeopleElected
#1: 1985 Bob (Liberal); Joe (Republican)
#2: 1980 Sarah (Green); Bill (Libertarian)
#3: 1987 Tom (Conservative)
或使用单个paste
setDT(df1)[, list(PeopleElected=paste(Name, ' (', Party, ')',
sep='', collapse='; ')) , by=FirstElected]
答案 1 :(得分:2)
采用dplyr
方式(因为我还没有说data.table
)
df1 <- data.frame(Name = c("Bob", "Joe", "Sarah", "Bill", "Tom"),
Party = c("Liberal", "Republican", "Green", "Libertarian",
"Conservative"),
FirstElected = c(1985, 1985, 1980, 1980, 1987))
df1 %>%
group_by(FirstElected) %>%
summarise(PeopleElected = paste0(paste0(Name, " (", Party, ")"),
collapse = "; "))
Source: local data frame [3 x 2]
FirstElected PeopleElected
(dbl) (chr)
1 1980 Sarah (Green); Bill (Libertarian)
2 1985 Bob (Liberal); Joe (Republican)
3 1987 Tom (Conservative)