简体   繁体   English

在R中的Dataframe中将多行合并为一行

[英]Combining multiple rows into one single row in Dataframe in R

Suppose I have a dataframe, which looks like this.假设我有一个数据框,它看起来像这样。

| | Category |分类 | Text |文字 |

| | :--------: | :--------: | :--------------------: | :--------------------: |

| | First |第一 | I am Groot.我是格鲁特。 | |

| | First |第一 | We are Groot.我们是格鲁特。 | |

| | Second |第二 | The Dark Knight Rises.黑暗骑士崛起。 | |

| | Second |第二 | I am Batman.我是蝙蝠侠。 | |

But we want to combine rows in column Text, which happens to have same value in category column, into one row and make it look like this.但是我们希望将 Text 列中的行(恰好在 category 列中具有相同的值)合并为一行,并使其看起来像这样。

| | Category |分类 | Text |文字 |

| | -------- | -------- | ------------------------------------ | ------------------------------------- |

| | First |第一 | I am Groot.我是格鲁特。 We are Groot.我们是格鲁特。 | |

| | Second |第二 | The Dark Knight Rises.黑暗骑士崛起。 I am Batman.我是蝙蝠侠。 | |

How do I do that?我怎么做?

data.table solution: data.table解决方案:

library(data.table)
dt0 <- data.table(
  Category = c(rep("First", 2), rep("Second", 2)),
  Text = c("I am Groot.", "We are Groot.", "The Dark Knight Rises.", "I am Batman.")
)
dt <- dt0[, .(Text = paste0(Text, collapse = " ")), by = .(Category)]
dt

Explanation: paste0 takes the column Text (which, in data.table syntax is evaluated to dt$Text ) and collapses it to a single value.说明: paste0获取列Text (在data.table语法中计算为dt$Text )并将其折叠为单个值。 This calculation is performed for each unique value in Category , indicated by by = .(Category) .Category每个唯一值执行此计算, by = .(Category)指示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM