parallel_gather: Melt multiple sets of columns in parallel

View source: R/utils.R

parallel_gatherR Documentation

Melt multiple sets of columns in parallel

Description

Essentially this is a wrapper around gather that is able to bind_cols with several gather operations. This is useful when a wide data frame contains uncertainty or flag information in paired columns.

Usage

parallel_gather(x, key, ..., convert = FALSE, factor_key = FALSE)

Arguments

x

A data.frame

key

Column name to use to store variables, which are the column names of the first gather operation.

...

Named arguments in the form new_col_name = c(old, col, names). All named arguments must have the same length (i.e., gather the same number of columns).

convert

Convert types (see gather)

factor_key

Control whether the key column is a factor or character vector.

Value

A gathered data frame.

See Also

gather

Examples

# gather paired value/error columns using
# parallel_gather
parallel_gather(pocmajsum,
  key = "param",
  value = c(Ca, Ti, V),
  sd = c(Ca_sd, Ti_sd, V_sd)
)

# identical result using only tidyverse functions
library(dplyr)
library(tidyr)
gathered_values <- pocmajsum %>%
  select(core, depth, Ca, Ti, V) %>%
  gather(Ca, Ti, V,
    key = "param", value = "value"
  )
gathered_sds <- pocmajsum %>%
  select(core, depth, Ca_sd, Ti_sd, V_sd) %>%
  gather(Ca_sd, Ti_sd, V_sd,
    key = "param_sd", value = "sd"
  )

bind_cols(
  gathered_values,
  gathered_sds %>% select(sd)
)


paleolimbot/mudata documentation built on Oct. 3, 2023, 10:03 a.m.