sfn_mutate_at: Mutate selected columns by function

Description Usage Arguments Details Value Examples

View source: R/sfn_dplyr.R

Description

Port of mutate_at for sfn_data and sfn_data_multi objects

Usage

1
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 indiviudal 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 Aug. 28, 2020, 1:13 a.m.