A function that matches or excludes column names in a data frame based on user-supplied sub-strings.

column(dtf, ...)

Arguments

dtf

A data frame.

...

Character strings with the sub-strings to match (or exclude) against the column names in dtf. If an entry starts with either !, ~, or -, any columns containing the substring will be excluded. Otherwise, the function will locate all column names containing all inputted sub-strings.

Value

A vector of column names meeting the inclusion and exclusion criteria.

Author

Kevin Potter

Examples

# Create a data frame
dtf <- data.frame(
  IDS.INT.Subject = rep( 1:4, each = 2 ),
  SSS.CHR.Group = rep( c( 'A', 'A', 'B', 'B' ), each = 2 ),
  SSS.INT.Group = rep( c( 1, 1, 2, 2 ), each = 2 ),
  SSS.LGC.Group_A = rep( c( T, T, F, F ), each = 2 ),
  SSS.CHR.Time_point = rep( c( 'Pre', 'Post' ), 4 ),
  SSS.INT.Time_point = rep( 0:1, 4 ),
  OUT.DBL.Scores = rnorm( 8 )
)

#' # All variables containing 'SSS'
column( dtf, 'SSS' )
#> [1] "SSS.CHR.Group"      "SSS.INT.Group"      "SSS.LGC.Group_A"   
#> [4] "SSS.CHR.Time_point" "SSS.INT.Time_point"

# All variables containing both 'SSS' and 'CHR'
column( dtf, 'SSS', 'CHR' )
#> [1] "SSS.CHR.Group"      "SSS.CHR.Time_point"

# Variables containing 'SSS' but not 'CHR'
column( dtf, 'SSS', '~CHR' )
#> [1] "SSS.INT.Group"      "SSS.LGC.Group_A"    "SSS.INT.Time_point"