Using tidyr
require(tidyr) # replicate data df <- structure(list(id1 = c(1, 1, 1, 2, 2), id2 = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c(" a ", " b "), class = "factor"), info = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c(" info1 ", " info2 "), class = "factor"), action_time = structure(1:5, .Label = c(" time1 ", " time2 ", " time3 ", " time4 ", " time5 " ), class = "factor")), .Names = c("id1", "id2", "info", "action_time" ), class = "data.frame", row.names = c(NA, -5L)) # create additional column on action_time sequence action_no <- paste("action_time", unlist(sapply(rle(df$id1)$lengths, function(x) seq(1, x)))) y <- cbind(df, action_no) # spread into final dataframe z <- spread(y, action_no, action_time)
Final conclusion
> z id1 id2 info action_time 1 action_time 2 action_time 3 1 1 a info1 time1 time2 time3 2 2 b info2 time4 time5 <NA>