0

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,])
}
Alex Holcombe
  • 2,453
  • 4
  • 24
  • 34

1 Answers1

0

Would this work ?

library(tidyverse)
map_if(d,is.matrix,~split(.,seq(nrow(.)))) %>% as_tibble
# # A tibble: 4 x 3
#       x y         z        
#   <dbl> <list>    <list>   
# 1     1 <dbl [3]> <dbl [3]>
# 2     2 <dbl [3]> <dbl [3]>
# 3     3 <dbl [3]> <dbl [3]>
# 4     4 <dbl [3]> <dbl [3]>

Maybe clearer if printed as a data.frame:

map_if(d,is.matrix,~split(.,seq(nrow(.)))) %>% as_tibble %>% print.data.frame
#   x          y          z
# 1 1  1, 12, 22  1, 12, 22
# 2 2  2, 13, 23  2, 13, 23
# 3 3  3, 20, 24  3, 20, 24
# 4 4 11, 21, 25 11, 21, 25
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167