我有一個數據幀如下融化重塑
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
只知道這是可以實現d正在重塑或融化,並從廣泛的格式轉變爲長格式,但不知道如何繼續進行。非常感謝任何幫助實現這一轉變。
你能做到這一點在'基地R'即'拍< - 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))]))) )'並更改列名稱 – akrun
或者另一個選項是從'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] [vari能夠] []' – akrun