On 14/02/2012 9:54 AM, Johannes Radinger wrote:
Hi,

I'd like to know if it is possible to use wildcards * for indexing...
E.g. I have a vector of strings. Now I'd like to select all elements
which start with A_*? I'd also need to combine that with logical operators:

"Select all elements of a vector that start with A (A*) OR that start with B 
(B*)"

Probably that is quite easy. I looked into grep() which I think might perform 
such tasks, but probably there is a more straigth forward solution.

a<- c("A_A","A_B","C_A","BB","A_Asd")
a[a=="A_A"| a=="A_B"] # here I'd like an index but with wildcard

Try grepl():

a[grepl("^[AB]", a)]

is probably the simplest way for your example.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to