【发布时间】:2021-05-08 00:53:38
【问题描述】:
我有一个很长的字符串向量,每个字符串由五个子字符串组成,每个字符串用下划线符号分隔:
例如,这里是字符串向量中的两个元素:
"land_somewhat_crop_produce_b.tif"
"marine_something_fish_meat_a.tif"
我想创建一个由这些子字符串组成的数据框。
| col1 | col2 | col3 | col4 | col5 |
|---|---|---|---|---|
| land | somewhat | crop | produce | b |
| marine | something | fish | meat | a |
使用正则表达式模式匹配,我如何提取每个下划线之间的每个子字符串并使用这些子字符串为每一行创建一个数据框?
【问题讨论】:
-
你可以做
data.frame(orig=c("land_somewhat_crop_produce_b.tif", "marine_something_fish_meat_a.tif")) %>% tidyr::separate(orig, into=paste0("col", 1:5))
标签: r regex dplyr tidyr stringr