Re: Counting elements in columns

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Counting elements in columns

Jim Silverton
I have two columns and I want to count hwo many pairs I have of the same
element. For example. If I have
a = c(1,2,3,4,5)
b= c(12, 1, 34, 2, 5, 0, 10, 100, 11)
It should return 3 (since 1, 2 and 5 occur in both columns).
Columns need not be of the same length.

--
Thanks,
Jim.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Counting elements in columns

djmuseR
length(intersect(a, b))


On Sat, Apr 16, 2011 at 8:04 PM, Jim Silverton <[hidden email]> wrote:

> I have two columns and I want to count hwo many pairs I have of the same
> element. For example. If I have
> a = c(1,2,3,4,5)
> b= c(12, 1, 34, 2, 5, 0, 10, 100, 11)
> It should return 3 (since 1, 2 and 5 occur in both columns).
> Columns need not be of the same length.
>
> --
> Thanks,
> Jim.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Counting elements in columns

helin_susam
In reply to this post by Jim Silverton
Hi,
try this;

sum(sapply(a, function(x) (x==b)))