An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120521/a434ffbd/attachment.pl>
all occurences of an element in a vector
5 messages · Ista Zahn, carol white, Joshua Wiley +1 more
Hi Carol, I'm not sure what a "sub-vector in a vector" is, but I think you might be looking for ?grep Best, Ista
On Mon, May 21, 2012 at 9:20 AM, carol white <wht_crl at yahoo.com> wrote:
Hi, How do you identify all occurences of an element or a sub-vector in a vector as opposed to match, %in%, and intersect which find the first occurrence of an element? Cheers, Carol ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120521/632575ff/attachment.pl>
On Mon, May 21, 2012 at 7:33 AM, carol white <wht_crl at yahoo.com> wrote:
like searching m or [m,n] in [1,n,m,e,m,n,n,u]. I want the exact match of all occurrences of m and n in the last vector. Therefore, grep is not helpful as it will extract if there are also mm and mmm.
I am pretty sure grep() is helpful if you are using the appropriate regular expression to search for. Like Ista, I am not sure exactly what your search criteria are. The easiest I think would be to give us some examples. Say give three vectors and what the desired output from your search is. Josh
Cheers, Carol
________________________________ ?From: Ista Zahn <istazahn at gmail.com> Cc: "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch> Sent: Monday, May 21, 2012 3:38 PM Subject: Re: [R] all occurences of an element in a vector Hi Carol, I'm not sure what a "sub-vector in a vector" is, but I think you might be looking for ?grep Best, Ista Hi, How do you identify all occurences of an element or a sub-vector in a vector as opposed to match, %in%, and intersect which find the first occurrence of an element? Cheers, Carol ? ? ? ?[[alternative HTML version deleted]] ______________________________________________ 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. ? ? ? ?[[alternative HTML version deleted]] ______________________________________________ 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.
Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
To convert this from the abstract to the real ... In what sense does %in% return only the first occurrence of an element? See:
foo <- c('1','n','m','e','m','n','n','u')
foo[foo %in% c('m','n')]
[1] "n" "m" "m" "n" "n"
cbind(foo, foo %in% c('m','n'))
foo [1,] "1" "FALSE" [2,] "n" "TRUE" [3,] "m" "TRUE" [4,] "e" "FALSE" [5,] "m" "TRUE" [6,] "n" "TRUE" [7,] "n" "TRUE" [8,] "u" "FALSE" It sure looks to me like %in% identifies all the occurrences of [m,n] in [1,n,m,e,m,n,n,u] Or did you mean [m,n] appearing next to each other in that order???
Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 5/21/12 7:33 AM, "carol white" <wht_crl at yahoo.com> wrote: >like searching m or [m,n] in [1,n,m,e,m,n,n,u]. > >I want the exact match of all occurrences of m and n in the last vector. >Therefore, grep is not helpful as it will extract if there are also mm >and mmm. > >Cheers, > >Carol > > >________________________________ > From: Ista Zahn <istazahn at gmail.com> > >Cc: "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch> >Sent: Monday, May 21, 2012 3:38 PM >Subject: Re: [R] all occurences of an element in a vector > >Hi Carol, > >I'm not sure what a "sub-vector in a vector" is, but I think you might >be looking for ?grep > >Best, >Ista > > >> Hi, >> How do you identify all occurences of an element or a sub-vector in a >>vector as opposed to match, %in%, and intersect which find the first >>occurrence of an element? >> >> Cheers, >> >> Carol >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. > [[alternative HTML version deleted]] >