1

I have a data set called click similar to this just larger:

> click <- data.frame(session = c(1,1,2,3,3), AppName = c("App1", "App2", "App3", "App2", "App1"), Step = c(1,2,1,1,2))
  session AppName Step
1       1    App1    1
2       1    App2    2
3       2    App3    1
4       3    App2    1
5       3    App1    2

From this data frame I want to create a new data frame where the first column is the session id and the following columns are the steps. The values should correspond to what happened in session x in step y, so the value of entrance [session = 1, step 2] should be App2 looking like this...

  session Step1 Step2
1       1  App1  App2
2       2  App3  <NA>
3       3  App2  App1

The example above has been made manually, however, my original data set is much larger, so I need another way of doing it. I have tried to look into the reshape package, however, I can't figure out if I can apply the functions to get what I want.

Furthermore, I have tried to create a new data frame and update the entrances using a for loop:

behaviour <- data.frame(matrix("", nrow = sessionNum, ncol = (stepNum + 1)))

behaviourNames <- c("Session")

for (i in 1:stepNum){
  behaviourNames <- c(behaviourNames, paste("Step", i, sep = ""))
}

colnames(behaviour) <- behaviourNames

stepNum <- max(click$Step)
for (i in 1:stepNum){
  levels(behaviour[,i+1]) <- c(levels(behaviour[,i+1]), nameList)
}

behaviour$Session <- sessions

for (i in 1:nrow(click){
  itSession <- click$Session[i]
  itStep <- click$Step[i]
  behaviour[,itStep + 1][behaviour$Session == itSession] <- click$AppName[i]
}

However, there's only one value in the levels of the step columns, so these can't be updated, hence, I thought there might be an easier way :)

lbl
  • 31
  • 2
  • 2
    `reshape2::dcast(click, session ~ Step, value.var="AppName")`, or `reshape(click, idvar="session", timevar="Step", direction="wide")` – user20650 Nov 26 '17 at 12:54
  • maybe mark as a dup : https://stackoverflow.com/questions/37332076/from-long-to-wide-data-with-multiple-columns/37332345 or https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – user20650 Nov 26 '17 at 12:57
  • Thank you so much, @user20650! I had simplified the example - the original data set has more columns. Thanks to your answer and the simplified example I realised why the method didn't work on the original data set :) Sometimes the solution is right in front of you even though you can't see it. – lbl Nov 26 '17 at 20:38

0 Answers0