sc: Search column names of a data frame

View source: R/sc.R

scR Documentation

Search column names of a data frame

Description

Lets you search the variable names of a data.frame for a particular string/pattern. A simple wrapper for the grep function.

Usage

sc(pattern, dataframe, value = TRUE, ignore.case = FALSE, ...)

Arguments

pattern

Character string containing the pattern you want to match. This is treated as a regular expression.

dataframe

A data.frame, or a matrix with column names

value

If TRUE returns the text of the matched values (default). If FALSE returns the indicies of the matched values.

ignore.case

If FALSE, the pattern matching is case sensitive (default)

Details

This is just a simple wrapper for the grep function. It searches through colnames(data.frame) for the pattern given.
If you don't want your code to be reliant on the DectechR package, you will get the same functionailty using something like:

grep("pattern", names(dataframe), value = TRUE)

Value

Returns a vector of column names that match the pattern given. If value == FALSE returns a vector of indicies of the matched columns.

Note

In Rstudio you can add a "code snippet" so that when you type "sc" and hit shift+tab, it inserts the equivalent base code into your script (i.e. grep("pattern", names(dataframe), value = TRUE)).
This means you still have the advantage of a quick to type 2-letter function, but you are just using a base R funcion (grep) and aren't tied to DectechR.

To do this go to Tools>Global Options>Code>Edit Snippets, and then add the following:
snippet sc grep("${1:pattern}", names(${2:dataframe}), value = TRUE)
...then at any time, in your script just type the letters "sc" followed by shift+tab.

See Also

See also grep and regex

Examples

    # search the "iris" data set for variables containing the word "Width"
    sc("Width", iris)

    # search the "mtcars" data set for variables ending with the letter "t"...
    sc("t$", mtcars)

    # get the indicies of all variables containing the letter "e"
    sc("e", mtcars, value = F)


Dectech/DectechR documentation built on Feb. 15, 2024, 9:17 a.m.