带列组的pivot_longer [英] pivot_longer with groups of columns
问题描述
我有一个看起来像这样的数据集:
I've got a dataset that looks like this:
df_start <- tribble(
~name, ~age, ~x1_sn_ctrl1, ~x1_listing2_2, ~x1_affect1, ~x2_sn_ctrl1, ~x1_listing2_2, ~x2_affect1, ~number,
"John", 28, 1, 1, 9, 4, 5, 9, 6,
"Paul", 27, 2, 1, 4, 1, 3, 3, 4,
"Ringo", 31, 3, 1, 2, 2, 5, 8, 9)
在处理内部分组时,我需要 pivot_longer()
我的列:
I need to pivot_longer()
while handling the groupings within my columns:
- 有2个x值(1和2)
- 有3个x值每个x值的问题(sn_ctrl1,listing2_2,ffect1)
在我的实际数据集中,有14个x。
In my actual dataset, there are 14 x's.
本质上,我想做的是将 pivot_longer()
应用于x值,但我的3个问题(sn_ctrl1,listing2_2,ffect1)宽。
Essentially, what I'd like to do is to apply pivot_longer()
to the x-values but leave my 3 questions (sn_ctrl1, listing2_2, affect1) wide.
我最后想得到的是:
df_end <- tribble(
~name, ~age, ~xval, ~sn_ctrl1, ~listing2_2, ~affect1, ~number,
"John", 28, 1, 1, 1, 9, 6,
"John", 28, 2, 4, 5, 9, 6,
"Paul", 27, 1, 2, 1, 4, 4,
"Paul", 27, 2, 1, 3, 3, 4,
"Ringo", 31, 1, 3, 1, 2, 9,
"Ringo", 31, 2, 2, 5, 8, 9)
我尝试了很多次尝试,但都失败了,尝试使用 names_pattern
& pivot_longer
,但完全被淘汰。
I have tried lots of very unsuccessful attempts playing with regex in names_pattern
& pivot_longer
but am completely striking out.
有人知道如何解决吗?
Anyone know how to tackle this?
谢谢!
PS:请注意,我试图举一个简单易懂的示例。我的列的实际名称略有不同。例如,有 x1_sn_ctrl1
& x1_attr1_ctrl2
。
PS: Note that I tried to make a straightforward reproducible example. The actual names of my columns vary slightly. For instance, there is x1_sn_ctrl1
& x1_attr1_ctrl2
.
推荐答案
您可以使用:
tidyr::pivot_longer(df_start,
cols = -c(name, age, number),
names_to = c("xval", ".value"),
names_pattern = 'x(\\d+)_(q\\d+)')
# A tibble: 6 x 7
# name age number xval q1 q2 q3
# <chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
#1 John 28 6 1 1 1 9
#2 John 28 6 2 4 5 9
#3 Paul 27 4 1 2 1 4
#4 Paul 27 4 2 1 3 3
#5 Ringo 31 9 1 3 1 2
#6 Ringo 31 9 2 2 5 8
这篇关于带列组的pivot_longer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!