Expanding columns associated with a categorical variable into multiple columns with dplyr/tidyr while retaining id variable [duplicate]
问题 This question already has an answer here : R spreading multiple columns with tidyr [duplicate] (1 answer) Closed last year . I have a data.frame that looks like this: dfTall <- frame_data( ~id, ~x, ~y, ~z, 1, "a", 4, 5, 1, "b", 6, 5, 2, "a", 5, 4, 2, "b", 1, 9) I want to turn it into this: dfWide <- frame_data( ~id, ~y_a, ~y_b, ~z_a, ~z_b, 1, 4, 6, 5, 5, 2, 5, 1, 4, 9) Currently, I'm doing this dfTall %>% split(., .$x) %>% mapply(function(df,name) {df$x <- NULL; names(df) <- paste(names(df),