Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

melt data.frames with non-atomic 'measure' columns #175

cnjr2 opened this issue Mar 24, 2016 · 2 comments

melt data.frames with non-atomic 'measure' columns #175

cnjr2 opened this issue Mar 24, 2016 · 2 comments


Copy link

cnjr2 commented Mar 24, 2016

Is it possible to gather() a data frame when it contains list elements?


data <- data_frame(
    name = c("Alex", "Alex", "Alex", "Tim", "Tim", "Tim"),
    year = c(1990, 1991, 1992, 1990, 1991, 1992),
    height = c(160, 165, 170, 120, 134, 150),
    weight = c(50, 52, 53, 48, 48, 52)

models <- data %>%
    group_by(name) %>%
    nest() %>%
        lm = map(data, ~lm(year ~ height, data = .)),
        lm2 = map(data, ~lm(year ~ height + weight, data = .))


This produces the following output:

   name           data      lm     lm2
  (chr)          (chr)   (chr)   (chr)
1  Alex <tbl_df [3,3]> <S3:lm> <S3:lm>
2   Tim <tbl_df [3,3]> <S3:lm> <S3:lm>

Now I would want to gather() this into a long format:

models %>% gather(variabe, value, -name, -data)

But I get:

Error: Can't melt data.frames with non-atomic 'measure' columns

I would like to end up with final data frame like this:

   name            data  variable    value
  (chr)           (chr)     (chr)    (chr)
1  Alex  <tbl_df [3,3]>        lm  <S3:lm>
2  Alex  <tbl_df [3,3]>       lm2  <S3:lm>
3   Tim  <tbl_df [3,3]>        lm  <S3:lm>
4   Tim  <tbl_df [3,3]>       lm2  <S3:lm>
@hadley hadley closed this as completed in bae80df May 16, 2016
Copy link

hadley commented May 16, 2016

Turned out to be way easier than I thought it would be!

Copy link

Hey I am also facing the same issue in my summer project. I would be immensely grateful if you share how you resolved it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

3 participants