comp_cols: Compare similarly named columns

Description Usage Arguments Value Examples

View source: R/comp_cols.R

Description

Extract columns with similar names and output a list of separate data.frames for easy comparison. The presence/absence of a prefix/suffix identifies similar colums, and the default corresponds to default-data.table joining (e.g., i.count, count). You can provide a list of column names for extraction instead.

Usage

1
2
3
4
5
6
7
8
comp_cols(
  x,
  id_cols = NULL,
  pattern = "^i\\.",
  col_list = NULL,
  restrict = FALSE,
  rm_na = FALSE
)

Arguments

x

a data.frame.

id_cols

character vector of any ID columns to retain in each data.frame output.

pattern

a regular expression pattern to match the distinguishing prefix/suffix.

col_list

a list of character(2) column-name pairs, Non-NULL values here take precedence over pattern.

restrict

one of "match", "mismatch", or FALSE, specifying whether to restrict the output to matches only, mismatches only, or not at all (the default).

rm_allna

logical indicating whether, in each set of comparison columns, all-NA rows should be removed.

Value

A list of data.frame objects.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
set.seed(575)

x <- data.frame(id = paste0("row_", 1:10),
                char = LETTERS[1:10],
                i.char = sample(LETTERS[1:10]),
                num = 1:10,
                i.num = sample(1:10)
                )

comp_cols(x)
comp_cols(x, id_cols = "id", restrict = "mismatch")

x[2, "char"] <- NA
x[2, "num"] <- NA
x[3, c("char", "i.char")] <- NA
x[5, c("num", "i.num")] <- NA

comp_cols(x)
comp_cols(x, id_cols = "id", rm_na = TRUE)

coletl/coler documentation built on May 12, 2021, 9:44 p.m.