Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 通过正则表达式匹配选择列_R_Dplyr_Stringr - Fatal编程技术网

R 通过正则表达式匹配选择列

R 通过正则表达式匹配选择列,r,dplyr,stringr,R,Dplyr,Stringr,我知道这方面有很多帖子,但我找不到任何有用的。我想做的很简单 我想根据列名中是否存在字母来选择(或删除)列 library(tibble) library(stringr) library(dplyr) xy <- tibble("x" = 1:5, "123" = 6:10, "x123" = 11:15) #does not have expected output > xy %>% select(-matches("[:alpha:]")) # A tibble: 5

我知道这方面有很多帖子,但我找不到任何有用的。我想做的很简单

我想根据列名中是否存在字母来选择(或删除)列

library(tibble)
library(stringr)
library(dplyr)

xy <- tibble("x" = 1:5, "123" = 6:10, "x123" = 11:15)

#does not have expected output
> xy %>% select(-matches("[:alpha:]"))
# A tibble: 5 x 3
      x `123`  x123
  <int> <int> <int>
1     1     6    11
2     2     7    12
3     3     8    13
4     4     9    14
5     5    10    15


如果可能的话,我正在寻找使用
stringr
dplyr
的解决方案。这似乎应该非常简单。

[:alpha://code>是一个POSIX字符类,它仅在括号内的表达式中有效。因此,您需要一对额外的括号:

xy <- tibble("x" = 1:5, "123" = 6:10, "x123" = 11:15)

xy %>% select(-matches("[[:alpha:]]"))
xy%select(-matches(“[:alpha:][]”)
结果:

# A tibble: 5 x 1
  `123`
  <int>
1     6
2     7
3     8
4     9
5    10
#一个tible:5 x 1
`123`
1     6
2     7
3     8
4     9
5    10

facepalm。好的,这很有效。我只是好奇为什么在使用
str\u view\u all
时不需要额外的括号。@LloydChristmas是一个很好的问题,我没有答案
[:alpha::
是所谓的POSIX字符类,仅在括号内的表达式中进行计算
[…]
[:alpha::
本身就意味着您正在按字面意思匹配字符
a
l
p
h
。这就是为什么让我困惑的原因,为什么您的
str\u view\u all
示例首先起作用。
# A tibble: 5 x 1
  `123`
  <int>
1     6
2     7
3     8
4     9
5    10