sc | R Documentation |
Lets you search the variable names of a data.frame for a particular string/pattern. A simple wrapper for the grep
function.
sc(pattern, dataframe, value = TRUE, ignore.case = FALSE, ...)
pattern |
Character string containing the pattern you want to match. This is treated as a |
dataframe |
A data.frame, or a matrix with column names |
value |
If |
ignore.case |
If |
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)
Returns a vector of column names that match the pattern given. If value == FALSE
returns a vector of indicies of the matched columns.
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 grep
and regex
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.