我有长格式的调查数据,我正试图将其转换为宽格式。由于到目前为止数据的编制方式,调查前和调查后的数据都有多列重复。还有多个需要旋转的列。根据此answer和评论,我认为我可以使用reshape2
来执行此操作,但这是我的第一次。尝试使用recast
函数时出现错误,但当我执行melt
和dcast
时,它会正常工作。
#Example code
test1 <- structure(list(Username = c("John", "John"), UserID = c(53, 53
), Pre.Test = c(0.5283, 0.5283), Post.Test = c(NA_real_, NA_real_
), Prepost = c("1", "2"), Q2_4 = c("1", "4"), Q2_7 = c("1", "5"
), Q2_11 = c("1", "7"), Q2_13 = c("1", "2")), .Names = c("Username",
"UserID", "Pre.Test", "Post.Test", "Prepost", "Q2_4", "Q2_7",
"Q2_11", "Q2_13"), row.names = 1:2, class = "data.frame")
test_long <- test1 %>% melt(id.vars = c("Username", "UserID", "Pre.Test",
"Post.Test", "Prepost"))
dcast(test_long, Username + UserID + Pre.Test + Post.Test ~ Prepost + variable)
我对id.vars
的解释是,它们是更改后每列保留的列。在dcast
函数中,它们位于公式的左侧,Prepost + variable
将调查问题与调查尝试结合起来。
然而,当我尝试recast
时,它不起作用:
recast(test1, Username + UserID + Pre.Test + Post.Test ~ Prepost + variable)
#For recast my understanding is that the left of the function will not change (like id.var)
#and the right of the function is what pivots
不可否认,我的代码可能是错误的,但我也想知道是否因为UserID是一个数字并被该函数错误地处理。我该怎么做才能获得这样的输出:
Username UserID Pre.Test Post.Test 1_Q2_4 1_Q2_7 1_Q2_11 1_Q2_13 2_Q2_4 2_Q2_7 2_Q2_11 2_Q2_13
1 John 53 0.5283 NA 1 1 1 1 4 5 7 2