I have a tibble where one of the columns is a vector list with equal length. I want to replace the last element of the list column with the maximums (by positions) of all vectors.
Eg.
my_tibble <- tibble(a = list(c(2, 4, 10, 10, 10), c(1, 12, 4, 8.5, 14), c(1,3,9, 15, 11)), b = c("a", "b", "c"))
I want to replace the last element of column a
with the maximums of all three elements by positions. The last list element of a
should now be
c(2, 12, 10, 15, 14)
How do I do this in a dplyr pipe? My all attempts using purrr::map
so far have failed and I don't think I am even getting closer. Any help is appreciated.
Do you want something like:
library(dplyr)
my_tibble |>
mutate(a = replace(a, row_number() == length(a), list(do.call(pmax, a))))
# A tibble: 3 × 2
a b
<list> <chr>
1 <dbl [5]> a
2 <dbl [5]> b
3 <dbl [5]> c
Where my_tibble$a
is:
[[1]]
[1] 2 4 10 10 10
[[2]]
[1] 1.0 12.0 4.0 8.5 14.0
[[3]]
[1] 2 12 10 15 14