Reshaping Data Cheat Sheet

Gather columns into rows.

tidyr::gather(cases, "year", "n", 2:4)

Spread rows into columns.

tidyr::spread(pollution, size, amount)

Combine vectors into data frame (optimized).

dplyr::data_frame(a = 1:3, b = 4:6)

Order rows by values of a column (low to high).

dplyr::arrange(mtcars, mpg)

Order rows by values of a column (high to low).

dplyr::arrange(mtcars, desc(mpg)) 

Rename the columns of a data frame.

dplyr::rename(tb, y = year)

Separate one column into several.

tidyr::separate(storms, date, c("y", "m", "d"))

Unite several columns into one.

tidyr::unite(data, col, ..., sep)

Comments