简体   繁体   English

如何使用正则表达式对 r 中的数据框列 select ?

[英]How can I use regular expression to select the columns of data frame in r?

I have the following data frame.我有以下数据框。

D <- data.frame(A123.case.vs.B123.Cntl._FC = 1:4, B123.case.vs.B123.Cntl._FC = 0:3, A123.case.vs.D123.Cntl._FC= 2:5, FC = 1:4, A123.case= 0:3, B123.cntrl = 0:3)

I would like to select the first 3 columns using regular expression.我想 select 使用正则表达式的前 3 列。

here is what I am trying but it does not work.这是我正在尝试的,但它不起作用。

D %>% select(contains("case.vs.[a-z]*[0-9].cntl"))

Do you have any idea where is my problem?你知道我的问题在哪里吗?

Thanks谢谢

The following regular expression does what the question asks for.以下正则表达式可以满足问题的要求。

library(dplyr)

D %>% select(matches('case\\.vs\\.[[:alpha:]][[:digit:]]+'))
#  A123.case.vs.B123.Cntl._FC B123.case.vs.B123.Cntl._FC A123.case.vs.D123.Cntl._FC
#1                          1                          0                          2
#2                          2                          1                          3
#3                          3                          2                          4
#4                          4                          3                          5

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

相关问题 使用正则表达式从R中的数据框列中提取子字符串 - Use a regular expression extract substring from data frame columns in R 用于过滤掉 R 数据框列的重复数字的正则表达式 - A regular expression to filter out repetitive numbers for R data frame columns 如何在数据框列的每个框中搜索正则表达式,然后仅返回匹配项而不是 R 中的整个字段? - How can I search for a regular expression in each box of data frame column, and then return just the match instead of the whole field in R? 如何通过与 R 中的另一个数据框匹配来选择矩阵中的所有行和列? - How can I select all rows and columns from a matrix by matching with another data frame in R? 如何从R中的数据框中选择和重命名列的长列? - How can I select and rename a long list of columns from a data frame in R? 如何在R中的数据框中有条件地选择列 - How to select columns conditionally in a data frame in R 如何在R中的数据框中更改行和列中的值? - How can I use if else to change values in some rows and columns in my data frame in R? 在R中-如何删除data.frame中的特定列? - in R - How can I remove specific columns in data.frame? 如何按 R 中的多列有条件地对数据框进行排序? - How can I sort a data frame conditionally by multiple columns in R? 如何在R中构建正则表达式? - How can I build regular expression in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM