I have a data frame like this:
colnames(df) <- c("A", "B", "C", "MO_A", "MO_B", "D", "F", "MO_D")
Now I wanna remove all the columns that start with "MO_"
What is the best way to do that?
I have a data frame like this:
colnames(df) <- c("A", "B", "C", "MO_A", "MO_B", "D", "F", "MO_D")
Now I wanna remove all the columns that start with "MO_"
What is the best way to do that?
Use a regex to get the columns of interest then index the data frame with a negative index to remove them.
With a logical index:
df <- df[, !grepl('MO_', colnames(df))]
With a numeric index:
df <- df[, -grep('MO_', colnames(df))]