rtry_trans_wider {rtry}R Documentation

Transform data from long to wide table

Description

This function transforms the original long table format of the data into a wide table format.

Usage

rtry_trans_wider(
  input,
  names_from = NULL,
  values_from = NULL,
  values_fn = NULL,
  showOverview = TRUE
)

Arguments

input

Input data frame or data table.

names_from

The column(s) from which the output column names to be obtained.

values_from

The column(s) from which the output values to be obtained.

values_fn

(Optional) Function to be applied to the output values.

showOverview

Default TRUE displays the dimension of the wide table.

Value

A data frame of the transformed wide table.

References

This function makes use of the pivot_wider function within the tidyr package.

See Also

rtry_select_row, rtry_select_col, rtry_select_anc, rtry_join_left

Examples

# Provide the standardized trait values per observation, together with species names
# and the georeferences of the sampling site (Latitude and Longtude), if availalbe,
# in a wide table format. Several steps are necessary:

# 1. Select only the trait records that have standardized numeric values.
#    The complete.cases() is used to ensure the cases are complete, i.e. have no
#    missing values.
num_traits <- rtry_select_row(data_TRY_15160,
                complete.cases(TraitID) & complete.cases(StdValue))

# 2. Select the relevant columns for transformation.
num_traits <- rtry_select_col(num_traits,
                ObservationID, AccSpeciesID, AccSpeciesName, TraitID, TraitName,
                StdValue, UnitName)

# 3. Extract the values of georeferences and the corresponding ObservationID.
lat <- rtry_select_anc(data_TRY_15160, 59)
lon <- rtry_select_anc(data_TRY_15160, 60)

# 4. Merge the relevant data frames based on the ObservationID using rtry_join_left().
num_traits_georef <- rtry_join_left(num_traits, lat, baseOn = ObservationID)
num_traits_georef <- rtry_join_left(num_traits_georef, lon, baseOn = ObservationID)

# 5. Perform wide table transformation of TraitID, TraitName and UnitName based on
#    ObservationID, AccSpeciesID and AccSpeciesName with cell values from StdValue.
#    If several records with StdValue were provided for one trait with the same
#    ObservationID, AccSpeciesID and AccSpeciesName, calculate their mean.
num_traits_georef_wider <- rtry_trans_wider(num_traits_georef,
                             names_from = c(TraitID, TraitName, UnitName),
                             values_from = c(StdValue),
                             values_fn = list(StdValue = mean))

# Expected messages:
# dim:   150 28
# dim:   150 7
# col:   ObservationID AccSpeciesID AccSpeciesName TraitID TraitName
#        StdValue UnitName
#
# dim:   98 2
# col:   ObservationID Latitude
#
# dim:   97 2
# col:   ObservationID Longitude
#
# dim:   150 8
# col:   ObservationID AccSpeciesID AccSpeciesName TraitID TraitName
#        StdValue UnitName Latitude
#
# dim:   150 9
# col:   ObservationID AccSpeciesID AccSpeciesName TraitID TraitName
#        StdValue UnitName Latitude Longitude
#
# dim:   146 7

# Learn more via the vignette (Workflow for general data preprocessing using rtry):
# vignette("rtry-workflow-general")

[Package rtry version 1.1.0 Index]