I use dplyr
, which requires that all dataframe or tibble columns be 1d atomic vectors or lists. But sometimes I have data frames with numeric array columns, like this:
d<- data.frame(x=c(1,2,3,4))
y= matrix(c(1,2,3, 11,12,13, 20,21,22, 23,24,25) ,nrow=4,ncol=3)
d$y = y
d$z= y
str(d)
# 'data.frame': 4 obs. of 3 variables:
# $ x: num 1 2 3 4
# $ y: num [1:4, 1:3] 1 2 3 11 12 13 20 21 22 23 ...
# $ z: num [1:4, 1:3] 1 2 3 11 12 13 20 21 22 23 ...
How can I convert all the non-atomic columns to lists?
Here is a non-vectorized way to convert one column, but I'd prefer vectorized code as my dataframes are fairly big:
d$l <- NaN
for (i in 1:nrow(d)) {
d[i,]$l <- list(d$y[i,])
}