简体   繁体   English

如何对R中的数据框的列名进行模糊匹配,因此可以一起选择它们

[英]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? 我想使用* _COST *删除所有列,如何一起执行?

你可以试试这个:

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

我会做这样的事情:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中使用模糊匹配来加入数据? - How can I join data using a fuzzy match in R? R编程:如何计算数据框中两个单元格之间的差异并将其保存在新列中 - R programming: How can I compute the difference between two cells in a data frame and save them in a new column 如何根据 R 中的另一个数据框匹配列的值并使用 dplyr 打印消息? - How can I match the values of a column according to another data frame in R and print a message using dplyr? 如何使用 dplyr 根据 R 中的另一个数据框匹配列的值? - How can i match the values of a column according to another of a data frame in R using dplyr? 如何匹配 R 数据框中的行 - How can I match-up rows in R data frame 如何创建标准尺寸的向量,以便可以将它们添加到数据框中? - How can I create vectors of a standard size so I can add them to a data frame? 我可以要求 R 识别数据框的名称,然后将该名称添加到同一数据框的列中吗? - Can I ask R to identify the name of a data frame and then add that name to a column in the same data frame? 将 for 循环的输出添加到 data.frame 中,以便我可以使用 r 中的 ggplot2 绘制它们 - Adding outputs from a for loop into a data.frame so i can graph them with ggplot2 in r 我怎样才能 select 仅从 R 中满足特定条件的数据框中的一列 - How can I select just one column from a data frame in R that meets certain condition R:如何在data.frame的行上使用apply并获取$ column_name? - R: How can I use apply on rows of a data.frame and get out $column_name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM