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