fccu_add: Atomic Vector and Matrix Addition with NA Tolerance

Description Usage Arguments Details Value Examples

View source: R/fc_common_utils.R

Description

On a list of matrices/data.frames or on a vector, perform addition while allowing for some NAs to be ignored. Useful if you want to address questions of prevalence where NA is a meaningful result that should not be propagated, but substituting it with 0, 1, etc. doesn't make sense.

Usage

1
fccu_add(r_obj, ratio = 0.5)

Arguments

r_obj

An atomic vector acceptable by sum, or a list of matrices or data.frames that have entries acceptable by sum. The latter case also requires that all list elements have the same dimensions.

ratio

How many NAs to tolerate as a ratio of vector length. In the case of matrix/data.frame addition, this is the vector in the third direction, i.e. the Z when the matrices/data.frames are stacked on top of one another.

Details

As a warning, this currently uses R for loops, so it's not fast.

Value

For an atomic vector, NA if NAs make up more than the ratio of the vector (i.e. >50 percent of the vector), otherwise the sum of the non-NA elements. For a list of matrices/data.frames, a matrix/data.frame equivalent to manually adding them together with, subject to the behavior for atomic vectors.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
> m1 = matrix(1, 3, 3)
> m2 = matrix(2, 3, 3)
> m0 = matrix(NA, 3, 3)

> fccu_add(list(m1,m2,m0), 0.5)
     [,1] [,2] [,3]
[1,]    3    3    3
[2,]    3    3    3
[3,]    3    3    3

> fccu_add(list(m1,m0,m0), 0.5)
     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA
[3,]   NA   NA   NA

> m0[1,] = 3
> m0[,3] = 5
> fccu_add(list(m1,m0,m0), 0.5)
     [,1] [,2] [,3]
[1,]    7    7   11
[2,]   NA   NA   11
[3,]   NA   NA   11

kmorrisongr/fcan documentation built on Sept. 9, 2020, 10:12 a.m.