To subset variables that have a pattern in R, you can use the grep()
function to find the indices of the variables that match the pattern and then use these indices for subsetting.
Here's an example:
main.r511 chars16 lines
In this example, we first create a sample data frame df
with 4 variables with names that contain the string "variable". We then use grep()
to find the indices of the variables that contain the pattern "variable". We save these indices in the variable_indices
variable. Finally, we subset the data frame df
using the identified variable indices to create a new data frame df_subset
that only contains the variables that match the pattern.
gistlibby LogSnag