add_zero: Expand a matrix or a data.frame with zeros based on rownames...

Description Usage Arguments Value Author(s) Examples

View source: R/utils.R

Description

For a given two-dimensional object with rownames and a character vector, add_zero produces a corresponding object whose rownames match the character vector, with zeros on the additional rows.

This function is an easy-to-use and reliable way to reintroduce non-responding units in the variance estimation process (after the non-response phase is taken into account).

Usage

1

Arguments

y

A (sparse) matrix or a data.frame. The object to add zeros to.

rownames

A character vector (other types are coerced to character). The character vector giving the rows of the produced object.

remove

Should rows of y whose name do not appear in the rownames argument be removed ? TRUE by default, a warning is shown when rows are removed.

Value

A (sparse) matrix or data.frame depending on the type of y.

Author(s)

Martin Chevalier

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Data generation
set.seed(1)
n <- 10
p <- 2
y <- matrix(1:(n*p), ncol = p, dimnames = list(sample(letters, n)))
y[c(3, 8, 12)] <- NA
rownames <- letters

# Standard use
add_zero(y, rownames)

# Use when rownames in y do not match
# any element in the rownames argument
rownames(y)[1:3] <- toupper(rownames(y)[1:3])
add_zero(y, rownames)
add_zero(y, rownames, remove = FALSE)

gustave documentation built on Nov. 10, 2021, 5:08 p.m.