[英]split column into 3 columns based on second and last underscore in r
How can we split a column into 3 columns based on the second and last underscore?我们如何根据第二个和最后一个下划线将一列拆分为 3 列?
library(tidyverse)
tbl = tibble(x = c("alpha_beta_gamma_delta", "a_b_c_d_e_h_2022", "hello123_stack_overflow_users"))
The desired result is below想要的结果如下
desired_result = tibble(
x1 = c("alpha_beta", "a_b", "hello123_stack"),
x2 = c("gamma", "c_d_e_h", "overflow"),
x3 = c("delta", "2022", "users")
)
The tidyr extract
function allows one to specify how you would like to split a string and how many columns you would like to return. tidyr
extract
function 允许指定您希望如何拆分字符串以及希望返回多少列。
Using your example, one can do:使用您的示例,可以执行以下操作:
library(tidyr)
extract(tbl, col = x, regex = "(.+?_.+?)_(.+)_(.+)", into = paste0("x", 1:3))
x1 x2 x3
<chr> <chr> <chr>
1 alpha_beta gamma delta
2 a_b c_d_e_h 2022
3 hello123_stack overflow users
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.