lookup.values: Generate function to fetch the value from specified...

Description Usage Arguments Value Examples

View source: R/counting_and_summarizing_tables.R

Description

A helper function to build df-specific search tools. These functions are typically used in conjunction with lapply to determine if they are present in a df.

Usage

1

Arguments

id

Used during function creation to customize which df columns are used for search. This can be a vector.

X

Search term, this can be a vector

dat

a data frame with both "id" and "field" columns

field

the search column name

Value

value from field corresponding to search terms. This function can select multiple field values that are collapsed with separator to conserve dimensionality

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  df <- data.frame(Patient.ID = c("p1", "p1", "p2", "p2", "p3"),
                   Visit      = c(1,     2,    1,    2,    NA),
                   Test.Result= c("neg","pos","neg", NA,   "pos"),
                   stringsAsFactors = F)
   lookup_by_patientid <- lookup.values("Patient.ID")
   lookup_by_patientid("patient2", df, "Test.Result")
   # "negative"
   lookup_by_patientid("patient1", df, "Test.Result")
   # "negative; positive"

   lookup_by_id_and_visit <- lookup.values(id = c("Patient.ID", "Visit"))
   lookup_by_id_and_visit(X = c("patient1", 2), dat = df, field = "Test.Result")
   # "positive"

   # get results for all patients on their first visit
   p <- strsplit(paste(unique(df$Patient.ID),1, sep = ";"), split = ";")
   unlist(lapply(p, lookup_by_id_and_visit, dat = df, field = "Test.Result"))
   # "negative" "negative"

dkrozelle/toolboxR documentation built on June 9, 2018, 8 a.m.