I have the following data:
Name <- c(\"Sam\", \"Sarah\", \"Jim\", \"Fred\", \"James\", \"Sally\", \"Andrew\", \"John\", \"Mairin\", \"Kate\", \"Sasha\", \
You can use which.min
and which.max
to get the first value.
data %>% group_by(Group) %>%
summarize(minAge = min(Age), minAgeName = Name[which.min(Age)],
maxAge = max(Age), maxAgeName = Name[which.max(Age)])
To get all values, use e.g. paste with an appropriate collapse
argument.
data %>% group_by(Group) %>%
summarize(minAge = min(Age), minAgeName = paste(Name[which(Age == min(Age))], collapse = ", "),
maxAge = max(Age), maxAgeName = paste(Name[which(Age == max(Age))], collapse = ", "))