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

Add '.id' argument to unnest #125

Closed
cashoes opened this issue Oct 19, 2015 · 6 comments

Comments

@cashoes
Copy link

commented Oct 19, 2015

When calling unnest on a named list, preserve names as an additional column. The name of the column should be specified by the .id argument.

l <- lapply(1:5, function(x) runif(5))
names(l) <- LETTERS[1:5]

df <- data.frame(x = 1:5, y = I(l))

unnest(df, y, .id = 'names')

# Source: local data frame [25 x 3]
# 
#        x names         y
#    (int) (chr)     (dbl)
#1      1     A 0.6019313
#2      1     A 0.6589553
#3      1     A 0.8539686
#4      1     A 0.3740433
#5      1     A 0.6602022
#6      2     B 0.2701723
#7      2     B 0.7914102
#8      2     B 0.6098436
#9      2     B 0.6916693
#10     2     B 0.3024860
# ..   ...   ...       ...

@hadley

This comment has been minimized.

Copy link
Member

commented Oct 19, 2015

This seems like a very reasonable parallel to dplyr::bind_rows().

@hadley

This comment has been minimized.

Copy link
Member

commented Dec 30, 2015

Hmmm, what happens if you're unnesting two named columns?

l <- lapply(1:5, function(x) runif(5))
names(l) <- LETTERS[1:5]

df <- dplyr::data_frame(x = 1:5, x = l, y = l)
unnest(df, x, y, .id = "names")
@lionel-

This comment has been minimized.

Copy link
Member

commented Dec 30, 2015

.id could be a vector as long as the number of columns to unnest.

@hadley

This comment has been minimized.

Copy link
Member

commented Dec 30, 2015

What if you want to use the names from one, but not another? I guess it could be a named vector, or you could use NAs.

@lionel-

This comment has been minimized.

Copy link
Member

commented Dec 30, 2015

You can also unnest in multiple steps in that case.

@hadley hadley closed this in f6d0365 May 18, 2016

@hadley

This comment has been minimized.

Copy link
Member

commented May 18, 2016

Currently takes single .id string - seems like the job of de-duplication is part of #184 (and if that's not enough, you can always do multiple unnests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.