How to drop columns by name pattern in R?

前端 未结 5 1966
忘掉有多难
忘掉有多难 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

    Just as an additional answer, since I stumbled across this, when looking for the data.table solution to this problem.

    library(data.table)
    dt <- data.table(df)
    drop.cols <- grep("1$", colnames(dt))
    dt[, (drop.cols) := NULL]
    

提交回复
热议问题