0

I have survey data in a long format and I'm trying to pivot it to wide format. Because of how the data has been compiled up to this point there are multiple columns that are duplicated for the pre- and post-survey data. There are also multiple columns that need to be pivoted. Based on this answer and comment I think I can use reshape2 to do this but it is my first time. When trying to use the recast function I get an error, but when I do melt and dcast it works fine.

#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)

My interpretation of the id.vars is that they are columns that will remain for each column after the change. In the dcast function they are on the left of the formula and Prepost + variable grabs the survey questions to pivot in combination with the survey attempt.

Yet, when I try recast it doesn't work:

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

Admittedly my code is probably wrong, but I also wondered if it is because the UserID is a number and being treated incorrectly by the function. What can I do to get output like this:

  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
Community
  • 1
  • 1
Andrew Jackson
  • 823
  • 1
  • 11
  • 23
  • 2
    You also need to specify an `id.var`. [See here for an example](http://stackoverflow.com/a/33168036/2204410) or `?recast` for the help-file. – Jaap Feb 01 '17 at 19:50
  • You also misspelled variable. – IRTFM Feb 01 '17 at 19:59
  • 1
    As a follow-up on my previous comment, you'll need: `recast(test1, Username + UserID + Pre.Test + Post.Test ~ Prepost + variable, id.var = c("Username", "UserID", "Pre.Test", "Post.Test", "Prepost"))` – Jaap Feb 01 '17 at 20:03
  • Thanks, so I just need to copy the left side of the equation to be the id.vars and I should be set. – Andrew Jackson Feb 01 '17 at 20:07

0 Answers0