【问题标题】:Multiple same rows to one row, but rows at different amount [duplicate]多行相同的行,但行的数量不同[重复]
【发布时间】:2021-06-24 11:06:41
【问题描述】:

我有这样的数据:

id word
1 bus
1 arrive
1 stop
1 time
1 beard
1 bearded
1 sits
2 whilst
2 argue
2 seat
2 time
2 police
3 officer
3 walks
3 intervenes

我想将其转换为如下数据集:

id word
1 arrive bus stop time beard bearded sits
2 whilst begin argue seat time
3 officer walks intervenes

有可能吗?

谢谢。

【问题讨论】:

标签: r text nlp


【解决方案1】:

为我的评论添加一些细节:

library(dplyr)

data <- tibble::tribble(
  ~id,        ~word,
   1L,     "arrive",
   1L,        "bus",
   1L,       "stop",
   1L,       "time",
   1L,      "beard",
   1L,    "bearded",
   1L,       "sits",
   2L,     "whilst",
   2L,      "begin",
   2L,      "argue",
   2L,       "seat",
   2L,       "time",
   2L,     "police",
   3L,    "officer",
   3L,      "walks",
   3L, "intervenes"
  )

data %>% 
  group_by(id) %>% 
  mutate(word = paste0(word, collapse = " ")) %>% 
  slice(1) %>% # Take the first line from each group
  ungroup()

或更好(所以你不需要切片):

data %>% 
  group_by(id) %>% 
  summarise(word = paste0(word, collapse = " "))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2017-04-11
    • 2023-02-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多