like: "a" %in% "abcd" TRUE Thanks.
I suppose here's one way:> hasChar <- function(x, y) { length(grep(x, y)) > 0 } > hasChar("a", "abcd")[1] TRUE> hasChar("e", "abcd")[1] FALSE Andy> From: Terry Mu > > like: > > "a" %in% "abcd" > TRUE > > Thanks. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
Terry Mu wrote on 4/2/2005 9:38 PM:> like: > > "a" %in% "abcd" > TRUE > > Thanks. >See ?regexpr. regexpr("a", "abcd") > 0 However, the first argument is not vectorized so you may also need something like: > sapply(c("a", "b", "e"), regexpr, c("abcd", "bcde")) > 0 a b e [1,] TRUE TRUE FALSE [2,] FALSE TRUE TRUE Be sure to read up on regular expressions if pursuing this option. HTH, --sundar
Sundar Dorai-Raj wrote:> > > Terry Mu wrote on 4/2/2005 9:38 PM: > >> like: >> >> "a" %in% "abcd" >> TRUE >> >> Thanks. >> > > > See ?regexpr. > > regexpr("a", "abcd") > 0 > > However, the first argument is not vectorized so you may also need > something like: > > > sapply(c("a", "b", "e"), regexpr, c("abcd", "bcde")) > 0 > a b e > [1,] TRUE TRUE FALSE > [2,] FALSE TRUE TRUEAlternatively you could use strsplit to split the original string into individual characters then fall back on %in%. The only complication is that strsplit will return a list of one character vector and you must unlist it to get the character vector itself. > strsplit("abcf", "") [[1]] [1] "a" "b" "c" "f" > "a" %in% unlist(strsplit("abcf", "")) [1] TRUE > "a" %in% unlist(strsplit("bcdf", "")) [1] FALSE