我有一个数据框如下
id gender group Student_Math_1 Student_Math_2 Student_Read_1 Student_Read_2
46 M Red 23 45 37 56
46 M Red 34 36 33 78
46 M Red 56 63 58
62 F Blue 59 68
62 F Blue 68 87 73
38 M Red 78 57 65
38 M Red 75 54
17 F Blue 74 56 72
17 F Blue 75 61 79
17 F Blue 74 43 81
df = structure(list(id = c(46, 46, 46, 62, 62, 38, 38, 17, 17, 17),
gender = structure(c(2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
1L), .Label = c("F", "M"), class = "factor"), group = structure(c(2L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L), .Label = c("Blue", "Red"
), class = "factor"), Student_Math_1 = c(23, 34, 56, 59,
NA, 78, NA, 74, 75, NA), Student_Math_2 = c(45, 36, 63, NA,
68, 57, 75, NA, 61, 74), Student_Read_1 = c(37, 33, 58, NA,
87, NA, 54, 56, NA, 43), Student_Read_2 = c(56, 78, NA, 68,
73, 65, NA, 72, 79, 81)), .Names = c("id", "gender", "group",
"Student_Math_1", "Student_Math_2", "Student_Read_1", "Student_Read_2"
), row.names = c(NA, -10L), class = "data.frame")
我想要做的是重塑这个数据集,使
Student_Math_1
和 Student_Math_2
列堆叠为单列 Math
一个在另一个下方,类似地,Student_Read_1
和 Student_Read_2
列堆叠为单列 Reading
,如下所示 id gender group Math Index1 Reading Index2
46 M Red 23 Student_Math_1 45 Student_Read_1
46 M Red 34 Student_Math_1 36 Student_Read_1
46 M Red 56 Student_Math_1 63 Student_Read_1
62 F Blue 59 Student_Math_1 Student_Read_1
62 F Blue Student_Math_1 68 Student_Read_1
38 M Red 78 Student_Math_1 57 Student_Read_1
38 M Red Student_Math_1 75 Student_Read_1
17 F Blue 74 Student_Math_1 Student_Read_1
17 F Blue 75 Student_Math_1 61 Student_Read_1
17 F Blue Student_Math_1 74 Student_Read_1
46 M Red 45 Student_Math_2 56 Student_Read_2
46 M Red 36 Student_Math_2 78 Student_Read_2
46 M Red 63 Student_Math_2 Student_Read_2
62 F Blue Student_Math_2 68 Student_Read_2
62 F Blue 68 Student_Math_2 73 Student_Read_2
38 M Red 57 Student_Math_2 65 Student_Read_2
38 M Red 75 Student_Math_2 Student_Read_2
17 F Blue Student_Math_2 72 Student_Read_2
17 F Blue 61 Student_Math_2 79 Student_Read_2
17 F Blue 74 Student_Math_2 81 Student_Read_2
只知道这可以通过重塑或熔化以及从宽格式更改为长格式来实现,不知道如何超越这一点。非常感谢对实现这种转变的任何帮助。
最佳答案
我们可以使用 melt
中的 data.table
library(data.table)
melt(setDT(df), measure = patterns("Math", "Read"),
value.name = c("Math", "Read"))[, Index1 := names(df)[4:5][variable]
][, Index2 := names(df)[5:6][variable]][]
或者另一种选择是
pat <- c("Student_Math", "Student_Read")
cbind(df[rep(1:nrow(df), 2), 1:3], do.call(cbind, lapply(pat,
function(nm) melt(df[grep(nm, names(df))]))))
关于r - 融化和重塑具有相似列根词的列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40541747/