Skip to content
Back to formatted view

Raw Message

Message-ID: <33ee61ee-9d45-3a14-5eb0-107b1dc37653@rgzm.de>
Date: 2020-08-20T06:38:04Z
From: Ivan Calandra
Subject: & and |
In-Reply-To: <CABcYAdJo6GskigEuAx+LRkQY87wk8bDL7OL2i2dx3xrXL4vDEQ@mail.gmail.com>

Thank you all for all the very helpful answers!

Best,
Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

On 20/08/2020 3:28, Richard O'Keefe wrote:
> There are & and | operators in the R language.
> There is an | operator in regular expressions.
> There is NOT any & operator in regular expressions.
> grep("ConfoMap&GuineaPigs", mydata, value=TRUE)
> looks for elements of mydata containing the literal
> string 'ConfoMap&GuineaPigs'.
>
> > foo <- c("a","b","cab","back")
> > foo[grepl("a",foo) & grepl("b",foo)]
> [1] "cab" ?"back"
>
> grepl returns a TRUE/FALSE vector.
>
> On Thu, 20 Aug 2020 at 02:53, Ivan Calandra <calandra at rgzm.de
> <mailto:calandra at rgzm.de>> wrote:
>
>     Dear useRs,
>
>     I feel really stupid, but I cannot understand why "&" doesn't work
>     as I
>     expect, while "|" does.
>
>     I have the following vector:
>     mydata <- c("SSFA-ConfoMap_GuineaPigs_NMPfilled.csv",
>     "SSFA-ConfoMap_Lithics_NMPfilled.csv",?
>     "SSFA-ConfoMap_Sheeps_NMPfilled.csv",
>     "SSFA-Toothfrax_GuineaPigs.xlsx",
>     "SSFA-Toothfrax_Lithics.xlsx", "SSFA-Toothfrax_Sheeps.xlsx")
>     and I want to find the values that include both "ConfoMap" and
>     "GuineaPigs".
>
>     If I do:
>     grep("ConfoMap&GuineaPigs", mydata, value=TRUE)
>     it returns an empty vector, character(0).
>
>     But if I do:
>     grep("ConfoMap|GuineaPigs", mydata, value=TRUE)
>     it returns all the elements that include either "ConfoMap" or
>     "GuineaPigs", as I would expect.
>
>     So what is wrong with my "&" construct? How can I return the elements
>     that include both parts?
>
>     Thank you for your help!
>     Ivan
>
>     -- 
>     Dr. Ivan Calandra
>     TraCEr, laboratory for Traceology and Controlled Experiments
>     MONREPOS Archaeological Research Centre and
>     Museum for Human Behavioural Evolution
>     Schloss Monrepos
>     56567 Neuwied, Germany
>     +49 (0) 2631 9772-243
>     https://www.researchgate.net/profile/Ivan_Calandra
>
>     ______________________________________________
>     R-help at r-project.org <mailto: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.
>