I often want to do tidyr::spreadit dplyr::summarisein one step to aggregate data into groups. What I want is shown in expected. I can get expectedit by doing summariseboth spreadseparately and combining the results with dplyr::full_join, but I'm looking for alternative approaches that avoid full_join . One-step bona fide approaches are not required.
df <- data.frame(
id = rep(letters[1], 2),
val1 = c(10, 20),
val2 = c(100, 200),
key = c("A", "B"),
value = c(1, 2))
library(tidyverse)
result1 <- df %>%
group_by(id) %>%
summarise(
val1 = min(val1),
val2 = max(val2)
)
result2 <- df %>%
select(id, key, value) %>%
group_by(id) %>%
spread(key, value)
expected <- full_join(result1, result2, by="id")
source
share