0

I manage to select all variables that contains an specific string in its name by:

df[grep("string1|string2",names(df))]

But what I really want to do is to keep those variables and drop the rest. I can do:

df2<-df[grep("string1|string2",names(df))]

But was wondering if I could simply drop those variables I am not interested in instead of creating another data frame. I tried:

null<-X_total[grep("!(mean|std)",names(X_total))

But did not work.

joan16v
  • 5,055
  • 4
  • 49
  • 49
Miguel Rayon Gonzalez
  • 1,513
  • 1
  • 11
  • 13

1 Answers1

1

Using dplyr, dropping all variables containing the strings Species or Sepal:

library(dplyr)
df <- select(iris, -matches("Species|Sepal"))

Output:

head(df, 3)
 Petal.Length Petal.Width
1          1.4         0.2
2          1.4         0.2
3          1.3         0.2
mpalanco
  • 12,960
  • 2
  • 59
  • 67