How do you "unnest" matrix columns in dplyr?
For example, what could I do with the y column to split it into 2 separate columns?
df <- data.frame(x = 1:2)
df$y <- matrix(1:4, ncol = 2)
How do you "unnest" matrix columns in dplyr?
For example, what could I do with the y column to split it into 2 separate columns?
df <- data.frame(x = 1:2)
df$y <- matrix(1:4, ncol = 2)
An easiest option is do.call with data.frame
df1 <- do.call(data.frame, df)
str(df1)
#'data.frame': 2 obs. of 3 variables:
# $ x : int 1 2
# $ y.1: int 1 2
# $ y.2: int 3 4
Or with tidyverse, we can split the 'y' to a list column and then use unnest_wider
library(dplyr)
library(purrr)
df %>%
mutate(y = asplit(y, 2)) %>%
unnest_wider(c(y))