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)

Arguments

table_long

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 second example).

column_to_repeat

name of the column of which the values have to be added to the column headings

columns_for_comparison

(vector with) name(s) of the column(s) you want to repeat for each value of column_to_repeat

Value

the dataframe in long format

Examples

library(forrescalc)
library(dplyr)
table_long <- read_forresdat_table(tablename = "dendro_by_plot_species") %>%
  filter(plot_id < 110) %>%
  select(plot_id, species, period, number_of_trees_ha, vol_alive_m3_ha)
#> Rows: 39 Columns: 16
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (1): plottype
#> dbl (15): plot_id, year, period, species, number_of_trees_ha, stem_number_ha...
#> 
#>  Use `spec()` to retrieve the full column specification for this data.
#>  Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> Rows: 17 Columns: 13
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (2): forest_reserve, plottype
#> dbl (4): plot_id, period, survey_number, year_dendro
#> lgl (7): survey_trees, survey_deadw, survey_veg, survey_reg, game_impact_veg...
#> 
#>  Use `spec()` to retrieve the full column specification for this data.
#>  Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> Warning: The dataset only contains presence data and lacks zero observations (except for 1 observation per plot_id and period to indicate that observations are done).  Please use function add_zeros() to add zero observations when needed.
table_wide <-
  make_table_wide(
    table_long, column_to_repeat = "period",
    columns_for_comparison = c("number_of_trees_ha", "vol_alive_m3_ha"))
#if number_of_trees_ha 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("vol_alive_m3_ha"))