Hello, this is a very simple question: How can I find out if a word is part of a list of words like: a <- "word1" b <- "word4" vector <- c("word1","word2","word3") I tried it with match(a,vector) but this gives the position of the word. I am not sure if and how that can be done with a logical operator like if: IF text is part of vector THEN print "is part" Probably a very easy thing to do, but I am missing the logical operator... and help(if) is not working best regards, johannes -- "Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Hi> Hello, > > this is a very simple question: > How can I find out if a word is part of a list of words > > like: > a <- "word1" > b <- "word4" > > vector <- c("word1","word2","word3") > > I tried it with match(a,vector) > but this gives the position of the word. >Perhaps a %in% vector Regards Petr> I am not sure if and how that can be > done with a logical operator like if: > IF text is part of vector THEN print "is part" > > Probably a very easy thing to do, but I am missing > the logical operator... and help(if) is not working > > best regards, > johannes > -- > "Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ... > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 20/01/12 12:50, Johannes Radinger wrote:> Hello, > > this is a very simple question: How can I find out if a word is > part of a list of words > > like: a <- "word1" b <- "word4" > > vector <- c("word1","word2","word3") > > I tried it with match(a,vector) but this gives the position of the > word. > > I am not sure if and how that can be done with a logical operator > like if: IF text is part of vector THEN print "is part" > > Probably a very easy thing to do, but I am missing the logical > operator... and help(if) is not workingcheck out %in% help: ?"%in%" Cheers, Rainer> > best regards, johannes- -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Stellenbosch University South Africa Tel : +33 - (0)9 53 10 27 44 Cell: +33 - (0)6 85 62 59 98 Fax : +33 - (0)9 58 10 27 44 Fax (D): +49 - (0)3 21 21 25 22 44 email: Rainer at krugs.de Skype: RMkrug -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk8ZV7IACgkQoYgNqgF2egroawCfYAN/eOBMKN4VDTbBZtiBVGdS LAUAnR+h9kg2INJTICiGIAUTfYm2fCbC =Ws2h -----END PGP SIGNATURE-----
Hi, thank you very much... %in% is the operator I was looking for. cheers, johannes -------- Original-Nachricht --------> Datum: Fri, 20 Jan 2012 13:01:54 +0100 > Von: Rainer M Krug <r.m.krug at gmail.com> > An: Johannes Radinger <JRadinger at gmx.at> > CC: R-help at r-project.org > Betreff: Re: [R] test if text is part of vector> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On 20/01/12 12:50, Johannes Radinger wrote: > > Hello, > > > > this is a very simple question: How can I find out if a word is > > part of a list of words > > > > like: a <- "word1" b <- "word4" > > > > vector <- c("word1","word2","word3") > > > > I tried it with match(a,vector) but this gives the position of the > > word. > > > > I am not sure if and how that can be done with a logical operator > > like if: IF text is part of vector THEN print "is part" > > > > Probably a very easy thing to do, but I am missing the logical > > operator... and help(if) is not working > > check out %in% > > help: > > ?"%in%" > > Cheers, > > Rainer > > > > > best regards, johannes > > > - -- > Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation > Biology, UCT), Dipl. Phys. (Germany) > > Centre of Excellence for Invasion Biology > Stellenbosch University > South Africa > > Tel : +33 - (0)9 53 10 27 44 > Cell: +33 - (0)6 85 62 59 98 > Fax : +33 - (0)9 58 10 27 44 > > Fax (D): +49 - (0)3 21 21 25 22 44 > > email: Rainer at krugs.de > > Skype: RMkrug > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.11 (GNU/Linux) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAk8ZV7IACgkQoYgNqgF2egroawCfYAN/eOBMKN4VDTbBZtiBVGdS > LAUAnR+h9kg2INJTICiGIAUTfYm2fCbC > =Ws2h > -----END PGP SIGNATURE------- "Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Jan-20 13:30 UTC
[R] test if text is part of vector
You also might look at grepl() if you have time: it allows regular expressions and will be a little (a lot?) more flexible in how you define a match if you want to ignore things like capitalization. (mnemonic: the L in grepl indicates its like grep but returns logicals instead of positions) Michael On Jan 20, 2012, at 7:42 AM, "Johannes Radinger" <JRadinger at gmx.at> wrote:> Hi, > > thank you very much... %in% is the operator I was looking for. > > cheers, > johannes > > -------- Original-Nachricht -------- >> Datum: Fri, 20 Jan 2012 13:01:54 +0100 >> Von: Rainer M Krug <r.m.krug at gmail.com> >> An: Johannes Radinger <JRadinger at gmx.at> >> CC: R-help at r-project.org >> Betreff: Re: [R] test if text is part of vector > >> -----BEGIN PGP SIGNED MESSAGE----- >> Hash: SHA1 >> >> On 20/01/12 12:50, Johannes Radinger wrote: >>> Hello, >>> >>> this is a very simple question: How can I find out if a word is >>> part of a list of words >>> >>> like: a <- "word1" b <- "word4" >>> >>> vector <- c("word1","word2","word3") >>> >>> I tried it with match(a,vector) but this gives the position of the >>> word. >>> >>> I am not sure if and how that can be done with a logical operator >>> like if: IF text is part of vector THEN print "is part" >>> >>> Probably a very easy thing to do, but I am missing the logical >>> operator... and help(if) is not working >> >> check out %in% >> >> help: >> >> ?"%in%" >> >> Cheers, >> >> Rainer >> >>> >>> best regards, johannes >> >> >> - -- >> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation >> Biology, UCT), Dipl. Phys. (Germany) >> >> Centre of Excellence for Invasion Biology >> Stellenbosch University >> South Africa >> >> Tel : +33 - (0)9 53 10 27 44 >> Cell: +33 - (0)6 85 62 59 98 >> Fax : +33 - (0)9 58 10 27 44 >> >> Fax (D): +49 - (0)3 21 21 25 22 44 >> >> email: Rainer at krugs.de >> >> Skype: RMkrug >> -----BEGIN PGP SIGNATURE----- >> Version: GnuPG v1.4.11 (GNU/Linux) >> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ >> >> iEYEARECAAYFAk8ZV7IACgkQoYgNqgF2egroawCfYAN/eOBMKN4VDTbBZtiBVGdS >> LAUAnR+h9kg2INJTICiGIAUTfYm2fCbC >> =Ws2h >> -----END PGP SIGNATURE----- > > -- > "Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ... > > ______________________________________________ > 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.