'R - Combine two elements within the SAME list. Preferentially a purrr solution
I am looking for a purrr solution for the following problem:
Say, we have some list:
list( c("Hello", "Well", "You" ),
c("again", "done,", "annoy"),
c("my friend!", "boy!", "me!" ) )
Now, I would like to to combine the the first two elements within that list.
My desired output is:
list( c("Hello", "Well", "You" , "again",
"done,", "annoy"),
c("again", "done,", "annoy"),
c("my friend!", "boy!", "me!" ) )
Appreciate your help! Thanks.
Solution 1:[1]
I don't think you want a purrr solution, but if you insist for some workflow reason...
x <- list(c("Hello", "Well", "You"),
c("again", "done,", "annoy"),
c("my friend!", "boy!", "me!"))
library(purrr)
modify_at(x, 1, ~ c(., x[[2]]))
# which can simplify to...
x %>%
modify_at(1, c, .[[2]])
# or with more purrr!!
x %>%
modify_at(1, c, pluck(., 2))
But I would just do...
x[[1]] <- c(x[[1]], x[[2]])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adam |
