【发布时间】:2019-10-31 22:33:40
【问题描述】:
我需要一个带有以下列的数据框df_wide:
userID SAT GRE task_conf task_chall active_conf active_chall sleep_conf sleep_chall morn_conf morn_chall
30798 A 1400 2 3 5 2 6 1 4 2
30895 A 1200 6 2 5 3 5 2 5 3
32678 B 1000 5 3 6 3 6 2 5 2
34679 A 1300 4 3 4 2 6 1 6 3
35999 A 1400 2 2 2 2 2 2 2 2
有关功能的一些信息:
The variables '_conf' and '_chall' contain integer values between 1 and 6
'userID's can be factors or integers but they are not continuous numbers
SAT represents the grade of that 'userID'
GRE represents the score of that 'userID'
SAT and GRE always stay the same for a given 'userID'
我的原始数据df_long目前格式如下:
userID SAT GRE action ConfChall vals
30798 A 1400 task conf 2
30798 A 1400 task chall 3
30798 A 1400 active conf 5
30798 A 1400 active chall 2
30798 A 1400 sleep conf 6
30798 A 1400 sleep chall 1
30798 A 1400 morn conf 4
30798 A 1400 morn chall 2
30895 A 1200 task conf 6
30895 A 1200 task chall 2
30895 A 1200 active conf 5
30895 A 1200 active chall 3
30895 A 1200 sleep conf 5
30895 A 1200 sleep chall 2
30895 A 1200 morn conf 5
30895 A 1200 morn chall 3
32678 B 1000 task conf 5
32678 B 1000 task chall 3
32678 B 1000 active conf 6
32678 B 1000 active chall 3
32678 B 1000 sleep conf 6
32678 B 1000 sleep chall 2
32678 B 1000 morn conf 5
32678 B 1000 morn chall 2
34679 A 1300 task conf 4
34679 A 1300 task chall 3
34679 A 1300 active conf 4
34679 A 1300 active chall 2
34679 A 1300 sleep conf 6
34679 A 1300 sleep chall 1
34679 A 1300 morn conf 6
34679 A 1300 morn chall 3
35999 A 1400 task conf 2
35999 A 1400 task chall 2
35999 A 1400 active conf 2
35999 A 1400 active chall 2
35999 A 1400 sleep conf 2
35999 A 1400 sleep chall 2
35999 A 1400 morn conf 2
35999 A 1400 morn chall 2
我尝试使用以下代码,但两种情况下的输出都不正确。
library(reshape2)
df_wide = recast(df_long, userID ~ c('action','confChall','vals'),
id.var = c("userID", "SAT", "GRE"))
df_wide = dcast(df_long, userID + SAT + GRE ~ c(action + ConfChall), value.var = "vals")
我尝试遵循以下页面中的示例代码。但是我很难将这些应用于我的问题。对此的任何建议或建议将不胜感激。
Reshape data from long to wide format - more than one variable
【问题讨论】:
-
?reshape2::melt