Hi everyone. I have a data set in which one of the columns is a list. I want to be able to split up that list into another data set, in which each element in the list becomes a new column in a new dataset. How would I go about that?
Can you explain what you want a bit more?
I've prepared a reprex that (I think) might answer your question, but you can also use it in order to clarify the question a bit more if that's not what you want:
library(magrittr)
input <- tibble::as_tibble(mtcars) %>%
dplyr::group_by(cyl) %>%
tidyr::nest()
input$data[[1]]
#> # A tibble: 7 x 10
#> mpg disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 21.4 258 110 3.08 3.22 19.4 1 0 3 1
#> 4 18.1 225 105 2.76 3.46 20.2 1 0 3 1
#> 5 19.2 168. 123 3.92 3.44 18.3 1 0 4 4
#> 6 17.8 168. 123 3.92 3.44 18.9 1 0 4 4
#> 7 19.7 145 175 3.62 2.77 15.5 0 1 5 6
purrr::map_df(input$data, ~.x)
#> # A tibble: 32 x 10
#> mpg disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 21.4 258 110 3.08 3.22 19.4 1 0 3 1
#> 4 18.1 225 105 2.76 3.46 20.2 1 0 3 1
#> 5 19.2 168. 123 3.92 3.44 18.3 1 0 4 4
#> 6 17.8 168. 123 3.92 3.44 18.9 1 0 4 4
#> 7 19.7 145 175 3.62 2.77 15.5 0 1 5 6
#> 8 22.8 108 93 3.85 2.32 18.6 1 1 4 1
#> 9 24.4 147. 62 3.69 3.19 20 1 0 4 2
#> 10 22.8 141. 95 3.92 3.15 22.9 1 0 4 2
#> # … with 22 more rows
Created on 2019-02-06 by the reprex package (v0.2.1)
Hey thanks. Essentially, I'm looking at a data set of lego purchases. There is a name for each purchase, but a person could have purchased multiple legos. The purchase
column is a list of 10 elements, and I want to make the 10 elements in that list into a data frame of 10 columns. I know I have to make the column of lists into a data frame and then use map_dfr to make them rows, but I'm not quite sure how to go about it.
Take a look at FAQ: What's a reproducible example (`reprex`) and how do I do one? and FAQ: How to do a minimal reproducible example ( reprex ) for beginners. It'll help everyone to solve the problem that you actually have. I'm sure, you can use datapasta
or something similar to create a couple rows dataset that illustrates your problem.
I am not sure exactly what you mean by "a list of 10 elements" (note that a data frame is itself a list of vectors) but generally I think that you might want something like the "dcast" function --- depending on what exactly you are trying to do.
E.g., one interpretation is that there are 10 different kind of lego packs, but the list is actually a column much longer than that, with only 10 unique elements. And then you want a data frame with a column per lego pack type, but what do you want the rows to be?
There are two useful function to translate a column of with different kind of elements into a set of columns, one for each element type --- it is called dcast --- and another, called melt, that does exactly the opposite, basically unite a set of columns into a single one, duplicating each row by the number of columns. I found these two very useful and it seems that they may be relevant to what you need, but I need some more precise description of your input data and what you'd like to be in your output (e.g. what should be the rows of the new data frame with a column per element type?)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.