selectValues: Select values from a vector, matrix or array

Description Usage Arguments Details Value See Also Examples

View source: R/array_general.R

Description

selectValues returns values (and their indices) of a vector, matrix, or array which meet the user-defined conditions.

Usage

1
2
3
4
5
6
7
8
selectValues(
  dat,
  condition,
  return_index = TRUE,
  dim_labels = NULL,
  value_label = "value",
  auto_convert = TRUE
)

Arguments

dat

an atomic object

condition

an IsFunction (see Details)

return_index

a logical value whether the indices of the selected values should be returned (default: TRUE)

dim_labels

a character vector; the name of the dimensions in the returned data.frame. If NULL (the default), the labels are extracted from the names of the dimnames attribute of the input data. If a single string, the dimension numbers are appended after it.

value_label

the name of the column in the returned data.frame which represent the values (default: "value")

auto_convert

a logical value whether automatic conversion of dimension names (i.e., characters to numeric (if possible) or to factors) should be performed (default: TRUE). Set to FALSE and call autoConvert directly on the returned data frame if you need more control.

Details

selectValues builds upon the is family of functions. The standard workflow is to define (and possibly combine) the conditions, and then feed the data into selectValues with the given condiiton object.

Value

This function returns a data frame with 'dim_labels' columns containing the combination of the dimension levels where the selected values were found and an extra column of the selected values themselves.
If 'return_index' is TRUE, the numeric matrix of indices is returned as an additional attribute ('index'). This comes handy if one wants to select data points in the same positions from an other object having the same shape as 'dat'.

See Also

is

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
#
# A complex use-case: find peaks of curves meeting compex rules
#
# load example data
data(erps)

# analyze only the three midline electrodes and use the
# grand averages, for simplicity
erps <- subsetArray(erps, chan = c("Fz", "Cz", "Pz"))
avgs <- avgDims(erps, "id")

# find the most negative peak at Fz between 80 and 200 ms, and also select
# the corresponding time points on the two other channels
cond <- isMinimum(options. = list(along_dim = "time"),
                  subset. = list(time = isBetween(80, 200)),
                  expand. = list(chan = "Fz")) &
        isNegative(expand. = list(chan = "Fz"))

# find the peaks
results <- selectValues(avgs, cond)

#
# plot the grand averages, and add points to the curves where a peak was found;
# note that the peaks are based on the Fz channel
#
library(ggplot2)
ggplot(transformArray(value ~ ., avgs), 
       aes(x = time, y = value, col = chan)) +
    geom_line() + facet_grid(stimclass ~ pairtype) + 
    geom_point(data = results, size = 3) + 
    theme_bw()

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