基于整数连接列字符串

时间:2015-09-21 10:13:40

标签: r

我想按照首先选出的日期创建字符串,如下所示。

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)"

我认为pasteapply/aggregate的某种组合可以做到这一点......但到目前为止我还没有多少运气。

2 个答案:

答案 0 :(得分:3)

我们可以使用paste/sprintf创建按' FirstElected'分组的格式。我们转换了' data.frame'到' data.table' (setDT(df1)),按' FirstElected'分组,我们将' Party'用括号括起来,用“' Name'使用sprintf,然后使用pastecollapse='; '创建一个字符串。

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)