如何将这个宽数据框转换为:
转换成这样?
# A tibble: 2 x 7
name question_1 question_1_response question_2 question_2_response question_3 question_3_response
<chr> <chr> <dbl> <chr> <dbl> <chr> <dbl>
1 ken PC1,PC2,PC4 4.5 PC3,MK1,MK2 3.5 SBP1,SBP5 5
2 hello PC1,PC5 4 MK1,SBP1,SBP2 4 NA NA
转换成这样?
# A tibble: 13 x 3
name subcomp value
<chr> <chr> <dbl>
1 ken PC1 4.5
2 ken PC2 4.5
3 ken PC4 4.5
4 ken PC3 3.5
5 ken MK1 3.5
6 ken MK2 3.5
7 ken SBP1 5
8 ken SBP5 5
9 hello PC1 4
10 hello PC5 4
11 hello MK1 4
12 hello SBP1 4
13 hello SBP2 4
示例数据:
library(tidyverse)
test <- tribble(
~name, ~question_1, ~question_1_response, ~question_2, ~question_2_response, ~question_3, ~question_3_response,
"ken", "PC1,PC2,PC4", 4.5, "PC3,MK1,MK2", 3.5, "SBP1,SBP5", 5,
"hello", "PC1,PC5", 4, "MK1,SBP1,SBP2", 4, NA, NA
)
我尝试使用gather/separate/spread,但无法完全理解它。
非常感谢!