[R] counting matched elements in two vectors

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Fri Jan 24 01:36:43 CET 2014


Thank you for the reproducible example, but your description is missing a clear definition of what you want.

For example, if your desired output is 
result <- c(rep(0,16),2,1,0,3,1,1,0,1,0,0,0,0,0,0,1,3,2,0,0,1,rep(0,10))

then one answer might be

as.vector(table(factor(w,levels=z)))

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

M.Bezabih at lse.ac.uk wrote:
>   Hi all,
>I have the following reproducible example
>
>z<-c(-5:40)
>w<-c(11, 11, 12, 14, 14, 14, 15, 16, 18, 25, 26, 26, 26, 27, 27, 30)
> r<-z %in% w
>
>now r gives me the presence or absence of elements in z that are in w
>but I am interested in getting the number of times each element in z
>appears (or doesn't appear)  in w. I want the dimension of my resulting
>vector to be the same as that of z. How do I do that?
>
> Thanks in advance
> Mintewab
>
>
>Please access the attached hyperlink for an important electronic
>communications disclaimer: http://lse.ac.uk/emailDisclaimer
>	[[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.




More information about the R-help mailing list