data_to_long {datawizard} | R Documentation |
This function "lengthens" data, increasing the number of rows and decreasing
the number of columns. This is a dependency-free base-R equivalent of
tidyr::pivot_longer()
.
data_to_long( data, select = "all", names_to = "name", names_prefix = NULL, names_sep = NULL, names_pattern = NULL, values_to = "value", values_drop_na = FALSE, rows_to = NULL, ignore_case = FALSE, regex = FALSE, ..., cols, colnames_to ) reshape_longer( data, select = "all", names_to = "name", names_prefix = NULL, names_sep = NULL, names_pattern = NULL, values_to = "value", values_drop_na = FALSE, rows_to = NULL, ignore_case = FALSE, regex = FALSE, ..., cols, colnames_to )
data |
A data frame to pivot. |
select |
Variables that will be included when performing the required tasks. Can be either
If |
names_to |
The name of the new column that will contain the column names. |
names_prefix |
A regular expression used to remove matching text from the start of each variable name. |
names_sep, names_pattern |
If |
values_to |
The name of the new column that will contain the values of the pivoted variables. |
values_drop_na |
If |
rows_to |
The name of the column that will contain the row names or row
numbers from the original data. If |
ignore_case |
Logical, if |
regex |
Logical, if |
... |
Currently not used. |
cols |
Identical to |
colnames_to |
Deprecated. Use |
If a tibble was provided as input, reshape_longer()
also returns a
tibble. Otherwise, it returns a 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 data frames: data_to_long()
, data_to_wide()
, data_rotate()
Functions to recode data: rescale()
, reverse()
, categorize()
, recode_values()
, slide()
Functions to standardize, normalize, rank-transform: center()
, standardize()
, normalize()
, ranktransform()
, winsorize()
Split and merge data frames: data_partition()
, data_merge()
Functions to find or select columns: data_select()
, data_find()
Functions to filter rows: data_match()
, data_filter()
wide_data <- data.frame(replicate(5, rnorm(10))) # Default behaviour (equivalent to tidyr::pivot_longer(wide_data, cols = 1:5)) data_to_long(wide_data) # Customizing the names data_to_long(wide_data, select = c(1, 2), names_to = "Column", values_to = "Numbers", rows_to = "Row" ) # Full example # ------------------ data <- psych::bfi # Wide format with one row per participant's personality test # Pivot long format data_to_long(data, select = regex("\\d"), # Select all columns that contain a digit names_to = "Item", values_to = "Score", rows_to = "Participant" ) data_to_long( tidyr::who, select = new_sp_m014:newrel_f65, names_to = c("diagnosis", "gender", "age"), names_pattern = "new_?(.*)_(.)(.*)", values_to = "count" )