我有这个数据框to_expand
有两列:
to_expand <- data.frame(first = c('a~b'), second = paste(list(c('1~2~3'), c('4~5~6')), collapse = '|'))
first second
1 a~b 1~2~3|4~5~6
我怎样才能把它变成:
# A tibble: 2 x 2
first second
<chr> <chr>
1 a 1~2~3
2 b 4~5~6
我尝试过使用sepratate_rows()
from tidyr
但它给出了两列之间所有可能的组合。
任何帮助将非常感激!
编辑:使用separate_rows(second, sep = '\\|')
给我a~b
在两行上。
> to_expand %>% separate_rows(second, sep = '\\|')
# A tibble: 2 x 2
first second
<chr> <chr>
1 a~b 1~2~3
2 a~b 4~5~6
如果我们使分隔符相同,我们可以更简单地做到这一点
library(dplyr)
library(tidyr)
library(stringr)
to_expand %>%
mutate(first = str_replace(first, "~", "|")) %>%
separate_rows(first, second, sep = "\\|")
# A tibble: 2 x 2
first second
<chr> <chr>
1 a 1~2~3
2 b 4~5~6
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)