Extract observations and/or variables from mfd objects.

# S3 method for mfd
[(mfdobj, i = TRUE, j = TRUE)

Arguments

mfdobj

An object of class mfd.

i

Index specifying functional observations to extract or replace. They can be numeric, character, or logical vectors or empty (missing) or NULL. Numeric values are coerced to integer as by as.integer (and hence truncated towards zero). The can also be negative integers, indicating functional observations to leave out of the selection. Logical vectors indicate TRUE for the observations to select. Character vectors will be matched to the argument fdnames[[2]] of mfdobj, i.e. to functional observations' names.

j

Index specifying functional variables to extract or replace. They can be numeric, logical, or character vectors or empty (missing) or NULL. Numeric values are coerced to integer as by as.integer (and hence truncated towards zero). The can also be negative integers, indicating functional variables to leave out of the selection. Logical vectors indicate TRUE for the variables to select. Character vectors will be matched to the argument fdnames[[3]] of mfdobj, i.e. to functional variables' names.

Value

a mfd object with selected observations and variables.

Details

This function adapts the fda::"[.fd" function to be more robust and suitable for the mfd class. In fact, whatever the number of observations or variables you want to extract, it always returns a mfd object with a three-dimensional coef array. In other words, it behaves as you would always use the argument drop=FALSE. Moreover, you can extract observations and variables both by index numbers and by names, as you would normally do when using [ with standard vector/matrices.

Examples

library(funcharts)
library(fda)

# In the following, we extract the first one/two observations/variables
# to see the difference with [.fd.
mfdobj <- data_sim_mfd()
fdobj <- fd(mfdobj$coefs, mfdobj$basis, mfdobj$fdnames) # The argument coef in fd # objects is converted to a matrix when possible. dim(fdobj[1, 1]$coef)
#> [1] 5 1
# Not clear what is the second dimension:
# the number of replications or the number of variables?
dim(fdobj[1, 1:2]$coef) #> [1] 5 2 dim(fdobj[1:2, 1]$coef)
#> [1] 5 2

# The argument coef in mfd objects is always a three-dimensional array.
dim(mfdobj[1, 1]$coef) #> [1] 5 1 1 dim(mfdobj[1, 1:2]$coef)
#> [1] 5 1 2
dim(mfdobj[1:2, 1]$coef) #> [1] 5 2 1 # Actually, [.mfd works as [.fd when passing also drop = FALSE dim(fdobj[1, 1, drop = FALSE]$coef)
#> [1] 5 1 1
dim(fdobj[1, 1:2, drop = FALSE]$coef) #> [1] 5 1 2 dim(fdobj[1:2, 1, drop = FALSE]$coef)
#> [1] 5 2 1