Given your data is ordered by userID
and sessionID
, and each row is a unique session, you could do:
library(data.table)
# Transform data into data.frame
df <- data.table(df)
df[, id := sequence(.N), by = c("userID")] # session sequence number per user
# Spread columns
reshape(df, timevar = "id", idvar = "userID", direction = "wide")
# userID date.1 sessionID.1 userType.1 date.2 sessionID.2 userType.2 date.3 sessionID.3 userType.3
#1 100105276 2015-01-01 1452632119 New Visitor 2015-01-02 1452634303 Returning Visitor 2015-01-02 1452637067 Returning Visitor
In this output userType
is also included as a variable, but you can always drop them afterwards.