如何在R中采用两列并按如下所示将它们展平?

时间:2019-02-23 19:39:41

标签: r dataframe

之前

+---------+------------------------------------+
|  Word   |                Tags                |
+---------+------------------------------------+
| morning | #sunrise #droplets #waterdroplets  |
| morning | #sky #ocean #droplets              |
+---------+------------------------------------+

之后

+---------+---------------+
|  Word   |     Tags      |
+---------+---------------+
| morning | sunrise       |
| morning | droplets      |
| morning | waterdroplets |
| morning | sky           |
| morning | ocean         |
| morning | droplets      |
+---------+---------------+

注意我要如何使液滴出现两次。该表很大,超过5m行,如果此方法有效,那将非常有帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

我们可以使用separate_rows中的tidyr

library(dplyr)
library(tidyr)

dat <- tribble(
  ~Word,   ~Tags,
  "morning", "#sunrise #droplets #waterdroplets",
  "morning", "#sky #ocean #droplets"
)

dat2 <- dat %>%
  separate_rows(Tags, sep = " #") %>%
  mutate(Tags = gsub("#", "", Tags))
dat2
# # A tibble: 6 x 2
#   Word    Tags         
#   <chr>   <chr>        
# 1 morning sunrise      
# 2 morning droplets     
# 3 morning waterdroplets
# 4 morning sky          
# 5 morning ocean        
# 6 morning droplets