pickcol: Pick elements from columns

pickcolR Documentation

Pick elements from columns

Description

This function will return colnames or column values (if value = TRUE) for "indicator-like" matrices or data frames.

Usage

pickcol(data, ind = 1L, value = FALSE, default = NA)

Arguments

data

a data frame or matrix

ind

if value = FALSE (default), a vector (usually a single value) to match and return column name(s) of data where ind is found; if value = TRUE, a vector of values to be ignored, e.g., NAs or empty strings

value

logical; if TRUE, returns column value(s); otherwise, returns column name(s) (default)

default

for value = FALSE, the default value returned if a row of data contains no ind in any column; for value = TRUE, the value returned if all data in a row is NA

Value

If value is FALSE (default), the column names of data for which each row of data contained ind.

If value is TRUE, the column values of data which are not values of ind.

Examples

set.seed(1)
ss <- sample(10)
dd <- as.matrix(ftable(1:10, ss))

all(pickcol(dd) == ss)

rn <- rnorm(10)
dd[dd == 1] <- rn
all(pickcol(dd, value = TRUE, ind = 0) == rowSums(dd))


dd <- data.frame(
  x = c(1, 0, 0, NA),
  y = c(0, 0, 1, NA),
  z = c(0, 1, 0, NA),
  a = c('one', '', '', NA),
  b = c('', '', 'three', NA),
  c = c('', 'two', '', NA)
)

pickcol(dd[1:2])
pickcol(dd[1:2], 0)
pickcol(dd[1:3] + 1, ind = 2)

pickcol(dd[4:6], value = TRUE, ind = '')
pickcol(dd, value = TRUE, ind = c('', 0:1))

dd[dd == ''] <- NA
pickcol(dd[4:6], value = TRUE)


raredd/rawr documentation built on April 29, 2024, 10:29 a.m.