You can try the dummies
library.
R Code:
# Creating the data frame
# id <- c(1,1,1,1)
# time <- c(1,2,3,4)
# data <- data.frame(id, time)
install.packages("dummies")
library(dummies)
data <- cbind(data, dummy(data$time))
Output:
id time data1 data2 data3 data4
1 1 1 0 0 0
1 2 0 1 0 0
1 3 0 0 1 0
1 4 0 0 0 1
Further you can rename the newly added dummy variable headers to suit your needs
R Code:
# Rename column headers
colnames(data)[colnames(data)=="data1"] <- "time_dummy_1"
colnames(data)[colnames(data)=="data2"] <- "time_dummy_2"
colnames(data)[colnames(data)=="data3"] <- "time_dummy_3"
colnames(data)[colnames(data)=="data4"] <- "time_dummy_4"
Output:
id time time_dummy_1 time_dummy_2 time_dummy_3 time_dummy_4
1 1 1 0 0 0
1 2 0 1 0 0
1 3 0 0 1 0
1 4 0 0 0 1
Hope this helps.