【问题标题】:Aggregating text in r data frame [duplicate]在r数据框中聚合文本[重复]
【发布时间】:2018-11-15 09:28:39
【问题描述】:

我有以下数据框。

x <- data.frame("Item" = c('Item1','Item1','Item2','Item2'), "Name" = c("This row and","the next should be grouped together","Also this row and","the next should be grouped together"))

我希望输出如下。

x2 <- data.frame("Item" = c('Item1','Item2'), "Name" = c("This row and the 
next should be grouped together","Also this row and the next should be 
grouped together"))

我有一个 10000 行的大型数据框,我是 R 新手。

【问题讨论】:

    标签: r dataframe aggregate


    【解决方案1】:

    您可以使用 dplyr 并将您的 Item 变量分组并连接文本。我被一个空字符串折叠,但是 ', ' 或 ';'也是可以的

    library(dplyr)
    
    x %>%
      group_by(Item) %>%
      summarise(Name=paste0(Name, collapse=''))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2016-01-13
      • 2020-11-21
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      相关资源
      最近更新 更多