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

Merge function #322

Closed
1danjordan opened this issue May 9, 2017 · 2 comments
Closed

Merge function #322

1danjordan opened this issue May 9, 2017 · 2 comments

Comments

@1danjordan
Copy link
Contributor

@1danjordan 1danjordan commented May 9, 2017

Saw this on HN today. The post is about the various methods of merging Maps (Dicts/Tables/...) in different languages. I especially liked the Clojure merge-with implementation.

In R, with identically sized lists A and B, Map(c, A, B) is a simple solution, or in purrr pmap(list(A, B), c).

Once the lists become more complicated, or the type of merge logic changes, this becomes much more complicated. For example, we might want to merge by name (key), position or just pack all elements together individually:

  A <- list(a = 1, b = "abc")
  B <- list(a = 2, b = "def", c = 1:5)
  C <- list(c = 6:10)

merge(list(A, B), .by = "name")
# => list(a = c(1, 2), b = c("abc", "def"), c = 1:5)

merge(list(A, C), .by = "position")
# => list(c(1, 6:10))
#          ^ strip names

merge(list(A, B, C))
# => list(a = 1, b = "abc", a = 2, b = "def", c = 1:5, c =  6:10)
# this is the same as flatten(list(A, B, C))

I figured there might be a place in purrr for merge.

@1danjordan 1danjordan closed this May 9, 2017
@1danjordan 1danjordan reopened this May 9, 2017
@hadley
Copy link
Member

@hadley hadley commented May 9, 2017

It seems like matching the list_modify() semantics for name vs. position would be most useful here.

Loading

@hadley hadley closed this in f355404 May 9, 2017
@1danjordan
Copy link
Contributor Author

@1danjordan 1danjordan commented May 9, 2017

Well that was spectacularly fast!

Loading

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

Successfully merging a pull request may close this issue.

None yet
2 participants