首页 > 解决方案 > 将一列中的单词分成一行

问题描述

我想拆分一列中的所有单词,以便它们各占一行(然后其余数据列重复)。

#Example Data

example_words <- c("one two three", "four five", "six")
values <- c(1, 2, 3)
tibble_test <- tibble(example_words, values)


# Expected output

example_words <- c("one",  "two", "three", "four", "five", "six")
values <- c(1, 1, 1, 2, 2, 3)

tibble_test <- tibble(example_words, values)

提前致谢

标签: rtidyverse

解决方案


这是否有效:

library(dplyr)
library(tidyr)
tibble_test %>% separate_rows(example_words, sep = ' ')
# A tibble: 6 x 2
  example_words values
  <chr>          <dbl>
1 one                1
2 two                1
3 three              1
4 four               2
5 five               2
6 six                3

推荐阅读