简体   繁体   中英

How can I do a fuzzy match for column name for data frame in R, so I can select them together

Say I have a dataframe like this

`str(data)
 $ ZN                        : int  1 1 2 2 2 3 3 4 4 5 ...
 $ X.AB.FFLOWS               : int  17 17 22 22 22 17 17 17 17 22 ...
 $ X.BA.FFLOWS               : int  17 17 22 22 22 17 17 17 17 22 ...
 $ AB_COSTAM                 : num  0.197 0.535 0.51 0.528 0.361 ...
 $ BA_COSTAM                 : num  0.197 0.535 0.51 0.528 0.361 ...
 $ AB_COSTMD                 : num  0.197 0.535 0.51 0.528 0.361 ...
 $ BA_COSTMD                 : num  0.197 0.535 0.51 0.528 0.361 ...
 $ AB_COSTPM                 : num  0.197 0.535 0.51 0.528 0.361 ...
 $ BA_COSTPM                 : num  0.197 0.535 0.51 0.528 0.361 ...
 $ AB_COSTNT                 : num  0.197 0.535 0.51 0.528 0.361 ...
 $ BA_COSTNT                 : num  0.197 0.535 0.51 0.528 0.361 ...`

I want to delete all the column with *_COST*, how can I do it together?

你可以试试这个:

data[,!grep!(".*_COST.*", colnames(data))]

我会做这样的事情:

data_reduced <- data[,-grep("_COST",colnames(data),fixed=T)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM