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 column combining argument to unnest() #184

Closed
mikabr opened this issue Apr 25, 2016 · 3 comments

Comments

@mikabr
Copy link

commented Apr 25, 2016

There are use cases where it seems like it would be useful to have some control over the column names that unnest() produces, for example if there are multiple columns of data frames with the same columns:

df <- dplyr::data_frame(
  x = 1:3,
  y = list(data_frame(a = runif(3), b = runif(3))),
  z = list(data_frame(a = runif(3), b = runif(3)))
)

tidyr::unnest(df)

# Source: local data frame [9 x 5]
#
#       x         a         b         a         b
#   (int)     (dbl)     (dbl)     (dbl)     (dbl)
#1     1 0.1828750 0.6893545 0.4887932 0.6530871
#2     1 0.5282101 0.1400115 0.5172123 0.4216703
#3     1 0.2047363 0.9180311 0.5185549 0.8593084
#4     2 0.1828750 0.6893545 0.4887932 0.6530871
#5     2 0.5282101 0.1400115 0.5172123 0.4216703
#6     2 0.2047363 0.9180311 0.5185549 0.8593084
#7     3 0.1828750 0.6893545 0.4887932 0.6530871
#8     3 0.5282101 0.1400115 0.5172123 0.4216703
#9     3 0.2047363 0.9180311 0.5185549 0.8593084

Maybe something like .combine, that combines the nested column names:

tidyr::unnest(df, .combine = TRUE)

# Source: local data frame [9 x 5]
#
#       x       y.a       y.b       z.a       z.b
#   (int)     (dbl)     (dbl)     (dbl)     (dbl)
#1     1 0.1828750 0.6893545 0.4887932 0.6530871
#2     1 0.5282101 0.1400115 0.5172123 0.4216703
#3     1 0.2047363 0.9180311 0.5185549 0.8593084
#4     2 0.1828750 0.6893545 0.4887932 0.6530871
#5     2 0.5282101 0.1400115 0.5172123 0.4216703
#6     2 0.2047363 0.9180311 0.5185549 0.8593084
#7     3 0.1828750 0.6893545 0.4887932 0.6530871
#8     3 0.5282101 0.1400115 0.5172123 0.4216703
#9     3 0.2047363 0.9180311 0.5185549 0.8593084

Or supplying a function for how to combine them?

@hadley

This comment has been minimized.

Copy link
Member

commented May 18, 2016

Do you have any examples of a similar API in other functions/packages? That would help me think about what would be most natural for tidyr.

@hadley hadley closed this in bee4bb5 May 22, 2016

@hadley

This comment has been minimized.

Copy link
Member

commented May 22, 2016

If you dislike this API, can you please let me know ASAP?

ldf <- list(data_frame(x = 1))
data_frame(x = ldf, y = ldf) %>%
  unnest(.sep = "_") %>%
  names()
#> [1] "x_x" "y_x"
@mikabr

This comment has been minimized.

Copy link
Author

commented May 22, 2016

That looks great.

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