Hey All, I am wondering if there is a built-in function allowing us to locate a particular word in a character vector. ex: vector a a [1] "superman" "xamn" "spiderman" "superman" "superman" "xman" [7] "spiderman" Is there any built-in function that can show "superman" are the first, fourth and fifith element in "a"? Please help me out. Thanks. -- View this message in context: http://www.nabble.com/locate-word-in-vector-tf4445881.html#a12685567 Sent from the R help mailing list archive at Nabble.com.
Is this what you want:> x <- c("superman" , "xamn" , "spiderman", "superman" , "superman" , "xman",+ "spiderman" )> which(x == "superman")[1] 1 4 5>On 9/14/07, kevinchang <shukai at seas.upenn.edu> wrote:> > Hey All, > > > I am wondering if there is a built-in function allowing us to locate a > particular word in a character vector. > > ex: vector a > > a > [1] "superman" "xamn" "spiderman" "superman" "superman" "xman" > [7] "spiderman" > > Is there any built-in function that can show "superman" are the first, > fourth and fifith element in "a"? Please help me out. Thanks. > > > -- > View this message in context: http://www.nabble.com/locate-word-in-vector-tf4445881.html#a12685567 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
kevinchang wrote:> Hey All, > > > I am wondering if there is a built-in function allowing us to locate a > particular word in a character vector. > > ex: vector a > > a > [1] "superman" "xamn" "spiderman" "superman" "superman" "xman" > [7] "spiderman" > > Is there any built-in function that can show "superman" are the first, > fourth and fifith element in "a"? Please help me out. Thanks.a <- c("superman", "xamn", "spiderman", "superman", "superman", "xman", "spiderman") grep("^superman$", a) [1] 1 4 5 ?grep OR which(a %in% "superman") [1] 1 4 5 ?which ?is.element -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894