make_table_wide: change dataframe from long to wide format

View source: R/make_table_wide.R

make_table_wideR Documentation

change dataframe from long to wide format

Description

This function changes a dataframe from long to wide format, e.g. to show data from 2 different periods in different columns.

Usage

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)
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"))


inbo/forrescalc documentation built on Sept. 28, 2024, 11:45 a.m.