我需要透视大量的列,这些列的名称不一定遵循某种模式。
为了进行说明,创建一个示例 Dataframe ,以及一个示例名称分组和一个示例ID变量(ID和ID2)的前两个组合的预期结果,我需要将它们保存在行中。
多谢了
# Example data frame
df <- data.frame(interview_id = c(1,1,1,2,2,3,3,3,4,4),
edad_l_id = c(1,2,3,1,2,1,2,3,1,2),
matrix(rnorm(200), ncol = 20))
# Example column names
colnames(df) <- c("ID","ID2","var1","ejem","exam","x1","sit","var2","ehem","pilot","y2","pot","var3","nom","ejec","z3","mar","var4","lom","yip","u4","tom")
# Example name list
namelist <- list(
group0 = c("ID","ID2"),
group1 = c("var1","ejem","exam","x1","sit"),
grupo2 = c("var2","ehem","pilot","y2","pot"),
grupo3 = c("var3","nom","ejec","z3","mar"),
grupo4 = c("var4","lom","yip","u4","tom"))
ID ID2 group1 group1_val group2 group2_val group3 group3_val group4 group4_val
1 1 var1 -1.37 var2 -0.23 var3 1.49 var4 -1.36
1 1 ejem -0.72 ehem 0.82 nom 0.37 lom 0.15
1 1 exam 0.49 pilot 0.18 ejec 0.69 yip -1.01
1 1 x1 -0.90 y2 -0.62 z3 0.48 u4 -0.93
1 1 sit 2.26 pot -1.10 mar 0.11 tom 0.77
1 2 var1 0.78 var2 -0.20 var3 0.36 var4 -0.59
1 2 ejem -0.76 ehem -1.29 nom 0.94 lom -0.59
1 2 exam -1.82 pilot -1.62 ejec 0.44 yip -0.04
1 2 x1 0.92 y2 0.39 z3 -0.69 u4 0.65
1 2 sit -1.05 pot 0.79 mar 1.01 tom -0.48
1 3 etc.........
等等等等2 1等等
1条答案
按热度按时间rqqzpn5f1#
也许有一种方法可以在一个非常花哨的旋转规范中完成这一切,但这似乎可以完成这项工作:
这个想法是分别透视每组变量,然后将它们合并回来。由于行之间没有唯一的标识符,我临时添加了第三个标识符变量,以避免重复的数据膨胀。