I'm trying to use purrr
to sum list elements with the same index. This can be achieved in base R using the following:
xx <- list(a = c(1,2,3,4,5), b = c(1,2,3,4,5))
Reduce("+", xx)
which provides:
[1] 2 4 6 8 10
Great! That's what I need, but I want to do it all in purrr
. %>% reduce(sum)
gives a single value back. Does anyone know the syntax to do this in purrr
?
Edit- I forgot to specify, this needs to work for n lists.
Dan
You can do (s. ?reduce
):
xx %>% reduce(`+`)
[1] 2 4 6 8 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With