Allow transposing a data.frame or tibble in a "tidy" way.
Instead of row names, the column names of the transposed object are taken from a specified id_col.
Note: The input df should not contain row names, as they would be discarded.
Usage
tidy_t(
df,
id_col = 1,
exclude_cols = NULL,
new_id_colname = NULL,
reguess_coltypes = FALSE
)Arguments
- df
The to-be-transposed data.frame or tibble.
- id_col
Which column of
dfshall be used as new column names? Can be either an index or a column name.- exclude_cols
Exclude specified columns before transposing. Can be either one or multiple indices or a column names, or
NULL.- new_id_colname
Rename the first column of the transposed object. If
NULL, carry over the selectedid_colcolumn name.- reguess_coltypes
Shall a re-guessing of column types be performed after transposing? Allows to correctly retrieve e.g. numeric or logical columns by utilizing
reguess_coltypes().
Examples
df <- data.frame(a = c("x1", "1", "FALSE"), b = c("x2", "2", "TRUE"))
tidy_t(df)
#> a x1 1 FALSE
#> 1 b x2 2 TRUE
# reguessing column types
tidy_t(df, reguess_coltypes = TRUE)
#> a x1 1 FALSE
#> 1 b x2 2 TRUE
# using second column as id column
tidy_t(df, id_col = "b")
#> b x2 2 TRUE
#> 1 a x1 1 FALSE
# excluding columns
tidy_t(df, exclude_cols = "b")
#> [1] a x1 1 FALSE
#> <0 rows> (or 0-length row.names)
# renaming id column
tidy_t(df, new_id_colname = "var")
#> var x1 1 FALSE
#> 1 b x2 2 TRUE