make_table_wide.Rd
This function changes a dataframe from long to wide format, e.g. to show data from 2 different periods in different columns.
make_table_wide(table_long, column_to_repeat, columns_for_comparison)
dataframe with data in long format. It is important that all columns that are not mentioned in the variables column_to_repeat
or columns_for_comparison
, are grouping variables that have the same value for each column_to_repeat
(see 2nd example).
name of the column of which the values have to be added to the column headings
(vector with) name(s) of the column(s) you want to repeat for each value of column_to_repeat
the dataframe in long format
if (FALSE) {
#change path before running
library(forrescalc)
library(dplyr)
table_long <-
read_forresdat(tablename = "dendro_status_tree", repo_path = "C:/gitrepo/forresdat") %>%
filter(plot_id < 110) %>%
select(plot_id, species, tree_id, period, dbh_mm, alive_dead)
table_wide <-
make_table_wide(table_long, column_to_repeat = "period",
columns_for_comparison = c("dbh_mm", "alive_dead"))
#if dbh_mm is not mentioned in columns_for_comparison, it is considered as a
#grouping variable while it has different values for each period.
#This gives an unwanted result with still many rows and a lot of NA values:
table_wide <-
make_table_wide(table_long, column_to_repeat = "period",
columns_for_comparison = c("alive_dead"))
}