[R] substring simultaneous conditions

Ulrik Stervbo ulrik.stervbo at gmail.com
Mon Sep 5 14:41:57 CEST 2016


Dear Rosa,

you can use grep for pattern matching along the lines of:

x <- c("000711111101", "000711111111", "000711111112","123456789123")

grep("^[0-9]{2}07[0-9]{6}(01|11)", x)

Here I assume that your real IDs consist of integers only. The pattern
matches two integers followed by 07 followed by 6 integers and finally 01
or 11

Hope this helps,
Ulrik

On Mon, 5 Sep 2016 at 13:54 Rosa Oliveira <rosita21 at gmail.com> wrote:

> Dear all,
>
> I have searched all over and didn´t found an answer :( Sorry, I'm new.
>
> I need urgently to "not analyse"the weight the ID's that have 07 in the
> position 3 and 4 respectively, 01 or 11 in positions 11 and 12 of ID
> variable. .
>
> I used the following code:
>
> base<--baseR[substr(baseR$'ID',3,4)!='03'
>                       &substr(baseR$'ID',11,12)!='01'
>                        &substr(baseR$'ID',11,12)!='11',]
>
> But, instead of removing just the id's that respect the 3 conditions
> simultaneously, base don't have all the id's that have 03 in the 3 and 4
> positions os id variable, neither 01  in positions 11 and 12 of ID,
> neither 11
> in positions 11 and 12 of ID variable.variable.
>
> So, it seems, that the code exclude all the conditions, as it was a OR (|)
> condition in spite of AND (&) condition.
>
> Can anyone help me please?
>
> I attach the data.
>
> Best,
> Rosa Oliveira
>
> __________________________________
>
> Antes de imprimir este e-mail pense bem se tem mesmo que o fazer.
> Há cada vez menos árvores.
> Não imprima, pense na sua responsabilidade e compromisso com o MEIO
> AMBIENTE!
> <
> http://pt.dreamstime.com/cora-ccedil-atildeo-criado-das-folhas-de-aacutervores-diferentes-thumb12275776.jpg
> >
> <
> http://pt.dreamstime.com/cora-ccedil-atildeo-criado-das-folhas-de-aacutervores-diferentes-thumb12275776.jpg
> >
> ______________________________________________
> 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list