Spread with duplicate identifiers (using tidyverse and %>%) [duplicate]

We can use tidyverse. After grouping by ‘start_end’, ‘id’, create a sequence column ‘ind’ , then spread from ‘long’ to ‘wide’ format library(dplyr) library(tidyr) df %>% group_by(start_end, id) %>% mutate(ind = row_number()) %>% spread(start_end, date) %>% select(start, end) # id start end #* <int> <fctr> <fctr> #1 2 1994-05-01 1996-11-04 #2 4 1979-07-18 NA #3 … Read more

Means multiple columns by multiple groups [duplicate]

We can use dplyr with summarise_at to get mean of the concerned columns after grouping by the column of interest library(dplyr) airquality %>% group_by(City, year) %>% summarise_at(vars(“PM25”, “Ozone”, “CO2”), mean) Or using the devel version of dplyr (version – ‘0.8.99.9000’) airquality %>% group_by(City, year) %>% summarise(across(PM25:CO2, mean))