[R] how to use AND in grepl

chalabi.elahe at yahoo.de chalabi.elahe at yahoo.de
Mon May 2 16:42:29 CEST 2016


Thanks Peter, you were right, the exact grepl is grepl("(.*t2.*pd.*)|(.*pd.*t2.*)",df$Command), but it does not change anything in Command, when I check the size of it by sum(grepl("(.*t2.*pd.*)|(.*pd.*t2.*)",df$Command))  the result is 0, but I am sure that the size is not 0. It seems that this AND does not work.
 

On Monday, May 2, 2016 5:05 AM, peter dalgaard <pdalgd at gmail.com> wrote:

On 02 May 2016, at 12:43 , ch.elahe via R-help <r-help at r-project.org> wrote:

> Thanks for your reply tom. After using  Subset(df,grepl("(.*t2.*pd.*)|(.*pd.*t2.*)"),df$Command)  I get this error: Argument "x" is missing, with no default. Actually I don't know how to fix this. Do you have any idea?

Tom's code was missing a ")" but not where you put one. He probably also didn't intend to capitalize "subset".


-pd

> Thanks,
> Elahe 
> 
> 
> On Saturday, April 30, 2016 7:35 PM, Tom Wright <tom at maladmin.com> wrote:
> 
> 
> 
> Actually not sure my previous answer does what you wanted. Using your approach:
> t2pd=subset(df,grepl("t2",df$Command) & grepl("pd",df$Command))
> Should work.
> I think the regex pattern you are looking for is:
> Subset(df,grepl("(.* t2.*pd.* )|(.* pd.* t2.*)",df$Command)
> 
> On Sat, Apr 30, 2016, 7:07 PM Tom Wright <tom at maladmin.com> wrote:
> 
> subset(df,grepl("t2|pd",x$Command))
>> 
>> 
>> 
>> 
>> On Sat, Apr 30, 2016 at 2:38 PM, ch.elahe via R-help <r-help at r-project.org> wrote:
>> 
>> Hi all,
>>> 
>>> I have one factor variable in my df and I want to extract the names from it which contain both "t2" and "pd":
>>> 
>>> 'data.frame': 36919 obs. of 162 variables
>>>  $TE                :int 38,41,11,52,48,75,.....
>>>  $TR                :int 100,210,548,546,.....
>>>  $Command          :factor W/2229 levels "_localize_PD","_localize_tre_t2","_abdomen_t1_seq","knee_pd_t1_localize","pd_local_abdomen_t2"...
>>> 
>>> I have tried this but I did not get result:
>>> 
>>> t2pd=subset(df,grepl("t2",Command) & grepl("pd",Command))
>>> 
>>> 
>>> does anyone know how to apply AND in grepl?
>>> 
>>> Thanks
>>> Elahe
>>> 
>>> ______________________________________________
>>> 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.
>>> .
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list