[R] Fwd: Grep command
Steven Yen
syen04 at gmail.com
Wed May 4 08:35:42 CEST 2016
Hi all
Both \\b and $ do the job. Thanks.
> x<-c("abc","def","rst","xyz","age","age2")
> x
[1] "abc" "def" "rst" "xyz" "age" "age2"
> grep("age2\\b",x)
[1] 6
> grep("age\\b",x)
[1] 5
> grep("age2$",x)
[1] 6
> grep("age$",x)
[1]
From: Jim Lemon <drjimlemon at gmail.com>
Date: Wed, May 4, 2016 at 2:19 AM
Subject: Re: [R] Grep command
To: Steven Yen <syen04 at gmail.com>
Does not:
abcplus<-c("zxzxabc","zxzxabc2rst")
grepl("abc",abcplus) & nchar(abcplus)<8
do the job?
Jim
On Wed, May 4, 2016 at 4:15 PM, Steven Yen <syen04 at gmail.com> wrote:
> Thanks but no.
> There will be time when I have
>
> zxzxabc and zxzxabc2rst
>
> and I don't like to grep with the string "abc" and end up with both.
>
>
> On Wed, May 4, 2016 at 2:02 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
>> Hi Steven,
>> If this is just a one-off, you could do this:
>>
>> grepl("age",x) & nchar(x)<4
>>
>> returning a logical vector containing TRUE for "age" but not "age2"
>>
>> Jim
>>
>>
>> On Wed, May 4, 2016 at 3:45 PM, Steven Yen <syen04 at gmail.com> wrote:
>>> Dear all
>>> In the grep command below, is there a way to identify only "age" and
>>> not "age2"? In other words, I like to greb "age" and "age2"
>>> separately, one at a time. Thanks.
>>>
>>> x<-c("abc","def","rst","xyz","age","age2")
>>> x
>>>
>>> [1] "abc" "def" "rst" "xyz" "age" "age2"
>>>
>>> grep("age2",x)
>>>
>>> [1] 6
>>>
>>> grep("age",x) # I need to grab "age" only, not "age2"
>>>
>>> [1] 5 6
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list