I would like to calculate the length of each element of a list within a data.table. Here is a small example.
test<-data.table(
x = list(c(1,2,3),c(1)),
y = list(c(2,3),c(1,2,3))
)
#This works:
test[,length_x:=sapply(x,length)]
test[,length_y:=sapply(y,length)]
#But this does not, can somebody explain why and how to correct it:
test[,c("length_x","length_y"):=sapply(.SD,length)]
Can somebody explain why the second part of the code does not work and how to correct it.