[R] count frequency

Rainer M Krug r.m.krug at gmail.com
Fri Sep 17 14:26:44 CEST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 17/09/10 14:19, Alaios wrote:
> Hello everyone,
> please consider the following lines of a matrix
> 
> 
> [574,]   59   32
> [575,]   59   32
> [576,]   59   32
> [577,]   59   32
> [578,]   59   32
> [579,]   59   32
> [580,]   59   32
> [581,]   60   32
> [582,]   60   33
> [583,]   60   33
> [584,]   60   33
> [585,]   60   33
> [586,]   60   33
> [587,]   60   33
> [588,]   60   33
> [589,]   60   33
> [590,]   60   33
> [591,]   61   33
> [592,]   61   33
> [593,]   61   33
> [594,]   61   33
> [595,]   61   33
> [596,]   61   33
> [597,]   61   33
> [598,]   61   33
> [599,]   61   33
> [600,]   61   33
> [601,]   62   34
> 
> Is it possible somehow to count the similarities between the first and second 
> column and put them on a third column like this?
> 
> 59 32 3
> 60 33 5
> 62 34 1
> 
> where (3,5,1 are the frequencies for (59,32), (60,33) and (62,34)

One solution:

> x <- data.frame(a=trunc(runif(10)*10), b=trunc(runif(10)*10))
> x
   a b
1  6 5
2  7 5
3  1 5
4  0 4
5  3 9
6  6 8
7  4 3
8  0 4
9  2 7
10 7 2
> x$comb <- paste(x$a, x$b)
> x
   a b comb
1  6 5  6 5
2  7 5  7 5
3  1 5  1 5
4  0 4  0 4
5  3 9  3 9
6  6 8  6 8
7  4 3  4 3
8  0 4  0 4
9  2 7  2 7
10 7 2  7 2
> table(x$comb)

0 4 1 5 2 7 3 9 4 3 6 5 6 8 7 2 7 5
  2   1   1   1   1   1   1   1   1
>

I am sure, there are better ones

Cheers,

Rainer

> 
> Best Regards
> Alex
> 
> 
> 
>       
> 	[[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.


- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Tel:        +33 - (0)9 53 10 27 44
Cell:       +27 - (0)8 39 47 90 42
Fax (SA):   +27 - (0)8 65 16 27 82
Fax (D) :   +49 - (0)3 21 21 25 22 44
Fax (FR):   +33 - (0)9 58 10 27 44
email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkyTXoQACgkQoYgNqgF2egrgrgCfUOvMaUOwOC7HPSdbC2fFzdLo
4scAmwQ2M1E000PH4HoQmkpkLbANtjLy
=B2y8
-----END PGP SIGNATURE-----



More information about the R-help mailing list