I want to extract the elements of a character array that contains some particular string. For example:
我想提取包含某些特定字符串的字符数组的元素。例如:
x <- c('aa', 'ab', 'ac', 'bb', 'bc')
I want some function such that, given x
and 'a'
(in general this can be a string), it returns 'aa', 'ab', 'ac'
. I have experimented with a combination of %in%
, match
, which
, etc, but have not been able to make them work. Any idea?
我想要一些函数,给定x和'a'(通常这可以是一个字符串),它返回'aa','ab','ac'。我已经尝试了%in,match,which等的组合,但是还没能使它们工作。任何想法?
1 个解决方案
#1
35
Just use grep
:
只需使用grep:
grep('a', x, value=TRUE)
[1] "aa" "ab" "ac"
#1
35
Just use grep
:
只需使用grep:
grep('a', x, value=TRUE)
[1] "aa" "ab" "ac"