write me how to keep all the column from a df containing the word "blank" in r

To keep only the columns from a data frame containing the word "blank", you can use the grep() function to find the columns' names that contain the word "blank", and then subset the original data frame based on that.

Here's an example code snippet:

main.r
# create an example data frame
df <- data.frame(id = 1:5, blank_1 = c(0, 1, 1, 0, 1), var_1 = rnorm(5), blank_2 = c(0.5, 0, 1, 0.5, 1), var_2 = rnorm(5))

# keep only the columns containing "blank"
df_blank <- df[, grep("blank", names(df))]
241 chars
6 lines

In this code, we first create an example data frame df with two columns containing the word "blank" (blank_1 and blank_2). We then use grep("blank", names(df)) to find the columns containing the word "blank", and subset the data frame using df[, grep("blank", names(df))] to keep only those columns. The resulting data frame df_blank will contain only the two columns with "blank" in their name (blank_1 and blank_2).

gistlibby LogSnag