I need to know which strings of an array that are in another array. What a best solution? Tks, Francisco. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Francisco JĂșnior, Computer Science - UFPE-Brazil "One life has more value that the world whole" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Francisco do Nascimento Junior <fnj at cin.ufpe.br> writes:> I need to know which strings of an array that are in another array. > What a best solution?%in%> c("foo", "bar", "baz") %in% c("foo", "bar", "boz", "bonz")[1] TRUE TRUE FALSE
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
Francisco, Is this what you're looking for?> aa[1] "ab" "cd" "de" "aa"> bb[1] "ab" "cd" "de" "df" "gg" "FF"> bb %in% aa[1] TRUE TRUE TRUE FALSE FALSE FALSE> aa %in% bb[1] TRUE TRUE TRUE FALSE OR> match(aa,bb)[1] 1 2 3 NA> match(bb,aa)[1] 1 2 3 NA NA If you have a string pattern, then you might also want to take a look at grep . HTH steve Francisco do Nascimento Junior wrote:> I need to know which strings of an array that are in another array. > What a best solution? > > Tks, > Francisco. > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > Francisco J?nior, > Computer Science - UFPE-Brazil > "One life has more value that the > world whole" > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help