sfn_mutate_at: Mutate selected columns by function

View source: R/sfn_dplyr.R

sfn_mutate_atR Documentation

Mutate selected columns by function

Description

Port of mutate_at for sfn_data and sfn_data_multi objects

Usage

sfn_mutate_at(sfn_data, .vars, .funs, ..., solar = FALSE)

Arguments

sfn_data

sfn_data or sfn_data_multi object to subset

.vars

Variables to mutate. Passed to mutate_at

.funs

Function/s for mutate, passed to mutate_at

...

Extra arguments to pass to the functions in .funs, passed to mutate_at.

solar

Logical indicating if solar timestamp must used to subset

Details

'sfn_mutate_at' function will maintain the same number of rows before and after the modification, so it is well suited to modify variables without creating TIMESTAMP gaps (i.e. to change variable units). For mutating individual variables see sfn_mutate.

Value

For sfn_data objects, a mutated sfn_data. For sfn_data_multi another sfn_data_multi with the sites mutated

Examples

library(dplyr)
library(lubridate)

# data
data('ARG_TRE', package = 'sapfluxnetr')

# transform to NAs any sapflow value occured with wind speed above 25
ws_threshold <- 25
# get the names of the variables to mutate (tree names)
vars_to_mutate <- names(get_sapf_data(ARG_TRE)[,-1]) # no TIMESTAMP

sfn_mutate_at(
  ARG_TRE,
  .vars = vars(one_of(vars_to_mutate)),
  .funs = list(
    ~ case_when(
      ws > ws_threshold ~ NA_real_,
      TRUE ~ .
    )
  )
)

## multi
data(ARG_MAZ, package = 'sapfluxnetr')
data(AUS_CAN_ST2_MIX, package = 'sapfluxnetr')
multi_sfn <- sfn_data_multi(ARG_TRE, ARG_MAZ, AUS_CAN_ST2_MIX)

## in multi it's better to discard the variables to not mutate:
vars_to_not_mutate <- names(get_env_data(ARG_TRE)) # all the environmental

multi_sfn_mutated <- sfn_mutate_at(
  multi_sfn,
  .vars = vars(-one_of(vars_to_not_mutate)), # we use -
  .funs = list(
    ~ case_when(
      ws > ws_threshold ~ NA_real_,
      TRUE ~ .
    )
  )
)

multi_sfn_mutated[['ARG_TRE']]


sapfluxnetr documentation built on Feb. 16, 2023, 7:52 p.m.