How to drop columns by name pattern in R?

前端 未结 5 1968
忘掉有多难
忘掉有多难 2020-11-28 08:23

I have this dataframe:

state county city  region  mmatrix  X1 X2 X3    A1     A2     A3      B1     B2     B3      C1      C2      C3

  1      1     1            


        
5条回答
  •  情歌与酒
    2020-11-28 08:36

    I found a simple answer using dplyr/tidyverse. If your colnames contain "This", then all variables containing "This" will be dropped.

    library(tidyverse) 
    df_new <- df %>% select(-contains("This"))
    

提交回复
热议问题