Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a data frame column containing a list into multiple columns using dplyr (or otherwise)

Tags:

r

dplyr

Consider the following example data

library(dplyr)
tmp <- mtcars %>%
    group_by(cyl) %>%
    summarise(mpg_sum = list(summary(mpg)))

such that mpg_sum contains the min, 1st quartile, median, mean, 3rd quartile, and max of the mpg variable by groups in cyl.

How do I unpack this column into 6 columns with appropriate column names with dplyr, or otherwise?

like image 872
Alex Avatar asked Jul 04 '16 06:07

Alex


People also ask

How do I split a column into multiple columns in R Dataframe?

To split a column into multiple columns in the R Language, we use the separator() function of the dplyr package library. The separate() function separates a character column into multiple columns with a regular expression or numeric locations.

How do I split a list into a Dataframe in R?

You can do this with read. table() in base R. Or with strcapture() . Or a simple call to tidyr::separate() with the help of stack() .

How do I split a column in R?

To divide each column by a particular column, we can use division sign (/). For example, if we have a data frame called df that contains three columns say x, y, and z then we can divide all the columns by column z using the command df/df[,3].


2 Answers

We can use data.table. Convert the 'data.frame' to 'data.table' (as.data.table(mtcars)), grouped by 'cyl', we get the summary of 'mpg' and convert it to list

library(data.table)
as.data.table(mtcars)[, as.list(summary(mpg)), by = cyl]
#    cyl Min. 1st Qu. Median  Mean 3rd Qu. Max.
#1:   6 17.8   18.65   19.7 19.74   21.00 21.4
#2:   4 21.4   22.80   26.0 26.66   30.40 33.9
#3:   8 10.4   14.40   15.2 15.10   16.25 19.2

Or using only dplyr, after grouping by 'cyl', we use do to do the same operation as above.

library(dplyr)
mtcars %>%
     group_by(cyl) %>%
     do(data.frame(as.list(summary(.$mpg)), check.names=FALSE) )
#   cyl  Min. 1st Qu. Median  Mean 3rd Qu.  Max.
#  <dbl> <dbl>   <dbl>  <dbl> <dbl>   <dbl> <dbl>
#1     4  21.4   22.80   26.0 26.66   30.40  33.9
#2     6  17.8   18.65   19.7 19.74   21.00  21.4
#3     8  10.4   14.40   15.2 15.10   16.25  19.2

Or using purrr

library(purrr)
mtcars %>% 
     slice_rows("cyl") %>% 
     select(mpg) %>%
     by_slice(dmap, summary, .collate= "cols")
like image 173
akrun Avatar answered Oct 10 '22 19:10

akrun


As commented, you can also use the tidy function from package broom:

library(broom)
mtcars %>% group_by(cyl) %>% do(tidy(summary(.$mpg)))
# Source: local data frame [3 x 7]
# Groups: cyl [3]
# 
#     cyl minimum    q1 median  mean    q3 maximum
#   (dbl)   (dbl) (dbl)  (dbl) (dbl) (dbl)   (dbl)
# 1     4    21.4 22.80   26.0 26.66 30.40    33.9
# 2     6    17.8 18.65   19.7 19.74 21.00    21.4
# 3     8    10.4 14.40   15.2 15.10 16.25    19.2
like image 26
talat Avatar answered Oct 10 '22 19:10

talat