[R] Find strings in a array

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Sat Feb 8 21:12:03 CET 2003


Francisco,

Francisco do Nascimento Junior wrote:
> I need to know which strings of an array that are in another array.
> What a best solution?
> 


Look at match, pmatch, %in%. Or for regular expressions, regexpr, grep.


For example,

R> x <- c("a", "b", "c")
R> y <- c("a", "b", "d")
R> z <- c("apple", "cantaloupe", "pear")
R> x %in% y
[1]  TRUE  TRUE FALSE
R> x[x %in% y]
[1] "a" "b"
R>
R> pmatch(x, z, nomatch = 0)
[1] 1 0 2
R> x[pmatch(x, z, nomatch = 0) > 0]
[1] "a" "c"
R>

Sundar




More information about the R-help mailing list