R语言 将列分成不同列

gupuwyp2  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(157)

一些示例数据:

df <- data.frame(xyz1_t1 = sample(1:5, size = 100, replace = TRUE), 
                 xyz1_t2 = sample(1:5, size = 100, replace = TRUE), 
                 xyz1_t3 = sample(1:5, size = 100, replace = TRUE),
                 xyz2_t1 = sample(1:5, size = 100, replace = TRUE), 
                 xyz2_t2 = sample(1:5, size = 100, replace = TRUE), 
                 xyz2_t3 = sample(1:5, size = 100, replace = TRUE),
                 ab1_t1 = sample(1:5, size = 100, replace = TRUE), 
                 ab1_t2 = sample(1:5, size = 100, replace = TRUE), 
                 ab1_t3 = sample(1:5, size = 100, replace = TRUE) 
)
> df %>% pivot_longer(cols = starts_with(c("xyz", "abc")))
# A tibble: 900 × 2
   name    value
   <chr>   <int>
 1 xyz1_t1     2
 2 xyz1_t2     4
 3 xyz1_t3     4
 4 xyz2_t1     2
 5 xyz2_t2     3
 6 xyz2_t3     3
 7 ab1_t1     2
 8 ab1_t2     2
 9 ab1_t3     5

如何将变量name拆分为三个变量scale(xyz,ab,...)、item(1,2,...)和time(1,2,3)?
多谢帮忙!

bqf10yzr

bqf10yzr1#

由于您已经在使用tidyverse,因此可以从tidyr使用extract

df |> tidyr::pivot_longer(cols = starts_with(c("xyz", "ab"))) |>
      tidyr::extract(name, 
               into=c("scale", "item", "time"), 
               regex="([a-z]+)([0-9]+)_t([0-9]+)",
               convert=TRUE,
               remove=FALSE)

# A tibble: 900 × 5
   name    scale  item  time value
   <chr>   <chr> <int> <int> <int>
 1 xyz1_t1 xyz       1     1     1
 2 xyz1_t2 xyz       1     2     3
 3 xyz1_t3 xyz       1     3     3
 4 xyz2_t1 xyz       2     1     3
 5 xyz2_t2 xyz       2     2     2
 6 xyz2_t3 xyz       2     3     3
 7 ab1_t1  ab        1     1     2
 8 ab1_t2  ab        1     2     1
 9 ab1_t3  ab        1     3     1
10 xyz1_t1 xyz       1     1     1
monwx1rj

monwx1rj2#

你试试看

library(stringr)
df2 <- df %>% pivot_longer(cols = starts_with(c("xyz", "ab"))) %>% 
mutate(scale=str_extract_all(name,'^\\w[a-zA-Z]*(?=\\d\\_)'),
 item=str_extract_all(name,'(?<=\\w[a-zA-Z]{1,3})\\d*(?=\\_)'),
time=str_extract_all(name,'(?<=\\_\\w)\\d*'))

相关问题