autoConvert: Convert (coerce) variables according to various schemes

Description Usage Arguments Details Examples

View source: R/array_transform.R

Description

autoConvert converts the variables which meet specific conditions on the basis of pre-defined conversion rules.

Usage

1
2
3
4
5
6
7
autoConvert(
  dat,
  select = NULL,
  conversion = "ANY",
  rules = convertParams(),
  keep_dim = TRUE
)

Arguments

dat

an object

select

numeric or character indices of the variables or list elements which sould be converted, if 'dat' is a data.frame or a list, respectively

conversion

a character vector referring to the conversion rule which should be applied for the selected variable/list element. If a single value, the same rule is applied for all variables/list elements. The default is 'ANY', which is handled in a special way (see Details).

rules

a list of conversion rules, see convertParams. To save typing, .(key = value) format is also accepted, and the arguments inside the parentheses are forwarded to convertParams.

keep_dim

a logical value whether the dimensions and dimension names should be retained after the conversion (default: TRUE)

Details

If 'conversion' is 'ANY' (the default), the 'IF' conditions in the rule definitions in 'rules' are tested and for the first successful test, the given rule is selected. To avoid these sequential tests, provide the name of the rule definition explicitly in 'conversion'. The rule definitions can be extended by arbitrary rules; it is suggested to prepare the rules by calling convertParams in advance (see Examples).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# create an example list with various variable types
x <- list(A = c(1L, 0L, 0L),  # integer, but could be simplified to logical 
          B = matrix(c(1, 3, 2, 1), 2, 2), # double, but could be integer  
          C = c("1.2", "1", "0.92"),   # character, which could be double
          D = factor(c("a", "b", "a"), levels = c("b", "a")) 
                 # factor, which might be converted to character
          )
( x_simplified <- autoConvert(x) )


# a simple way to convert only those list elements which are factors
autoConvert(x, conversion = "factor")

# suppose you do not want to convert characters at all
rules <- convertParams()
new_rules <- rules[setdiff(names(rules), "character")]
( x_simplified2 <- autoConvert(x, rules = new_rules) )


# you can also convert an atomic object
autoConvert(c("1.12", "3.4"))

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.