I have a data table as below:
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
I want to add three more columns day,month,year to this column.So I tried
dt[,day:=unlist(strsplit(date,"\\ "))[1]]
dt[,month:=unlist(strsplit(date,"\\ "))[2]]
dt[,year:=unlist(strsplit(date,"\\ "))[3]]
But this just appended all columns with same split for the first row.
SoI tried
dt[,day:=lapply(date,function (x) {unlist(strsplit(x,"\\ "))[1]}),]
dt[,month:=lapply(date,function (x) {unlist(strsplit(x,"\\ "))[2]}),]
dt[,year:=lapply(date,function (x) {unlist(strsplit(x,"\\ "))[3]}),]
which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated