Re: [R] Counting

From: K. Elo <maillists_at_nic.fi>
Date: Thu, 17 Mar 2011 21:51:01 +0200

Dear Jim,

17.03.2011 20:54, Jim Silverton wrote:
> I have a matrix say:
>
> 23 1
> 12 12
> 0 0
> 0 1
> 0 1
> 0 2
> 23 2
>
> I want to count of number of distinct rows and the number of disinct element
> in the second column and put these counts in a column. SO at the end of the
> day I should have:
>
> c(1, 1, 1, 2, 2, 1, 1) for the distinct rows...

Let's suppose my.data is your data frame, "var" is the 1st column and "var1" is the second.

1. Create a 3rd columns for the first task: my.data\$var2<-0
2. Count distinct rows:

for (i in 1:nrow(my.data)) { my.data\$var2[i]<-nrow(subset(my.data, var==var[i] & var1==var1[i])) }

After this, the output of "my.data\$var2" is:

[1] 1 1 1 2 2 1 1

> ... and c(1, 1, 1, 2, 2, 2, 2) for the counts of how many times the  > elements in the second column exists.

Here I'm a bit irritated. Shouldn't the count for the first element "1" rather be 3, since the number 3 occurs three times... If this is what You are looking for, then the following should work:

1. Create a 4th column for: my.data\$var3<-0
2. Count distinct elements in the second column:

for (i in 1:nrow(my.data)) {
my.data\$var3[i]<-sum(my.data\$var1==my.data\$var1[i]) }

After this, the output of "my.data\$var3" is:

[1] 3 1 1 3 3 2 2

HTH,
Kimmo

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. Received on Thu 17 Mar 2011 - 19:55:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 17 Mar 2011 - 20:00:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.