This function rotates a data frame, i.e. columns become rows and vice versa.
It's the equivalent of using t()
but restores the data.frame
class,
preserves attributes and prints a warning if the data type is
modified (see example).
data_rotate(data, rownames = NULL, colnames = FALSE, verbose = TRUE)data_transpose(data, rownames = NULL, colnames = FALSE, verbose = TRUE)
A data frame.
Character vector (optional). If not NULL
, the data frame's
rownames will be added as (first) column to the output, with rownames
being the name of this column.
Logical or character vector (optional). If TRUE
, the values
of the first column in x
will be used as column names in the rotated data
frame. If a character vector, values from that column are used as column
names.
Toggle warnings.
A (rotated) data frame.
Functions to rename stuff: data_rename()
, data_rename_rows()
, data_addprefix()
, data_addsuffix()
Functions to reorder or remove columns: data_reorder()
, data_relocate()
, data_remove()
Functions to reshape, pivot or rotate dataframes: data_to_long()
, data_to_wide()
, data_rotate()
Functions to rescale and reverse: data_rescale()
, data_reverse()
Functions to standardize, normalize, rank-transform: standardize()
, normalize()
, ranktransform()
, winsorize()
Split, cut and merge dataframes: data_partition()
, data_cut()
, data_merge()
Functions to find or select columns: find_columns()
Functions to filter rows: data_match()
, data_filter()
# NOT RUN {
x <- mtcars[1:3, 1:4]
x
data_rotate(x)
data_rotate(x, rownames = "property")
# use values in 1. column as column name
data_rotate(x, colnames = TRUE)
data_rotate(x, rownames = "property", colnames = TRUE)
# warn that data types are changed
str(data_rotate(iris[1:4, ]))
# use either first column or specific column for column names
x <- data.frame(a = 1:5, b = 11:15, c = letters[1:5], d = rnorm(5))
data_rotate(x, colnames = TRUE)
data_rotate(x, colnames = "c")
# }
Run the code above in your browser using DataLab