一些示例数据:
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)?
多谢帮忙!
2条答案
按热度按时间bqf10yzr1#
由于您已经在使用
tidyverse
,因此可以从tidyr
使用extract
monwx1rj2#
你试试看