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) Best Regards Alex [[alternative HTML version deleted]]
-----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)) > xa 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) > xa 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-----
Try this: aggregate(rep(1, nrow(x)), x, sum) On Fri, Sep 17, 2010 at 9:19 AM, Alaios <alaios@yahoo.com> 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) > > Best Regards > Alex > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On 17-Sep-10 12:19:10, 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) > > Best Regards > AlexDoes the following do what you want? a <- c(59,59,59,59,59,59,59,60,60,60,60,60,60,60, 60,60,60,61,61,61,61,61,61,61,61,61,61,62) b <- c(32,32,32,32,32,32,32,32,33,33,33,33,33,33, 33,33,33,33,33,33,33,33,33,33,33,33,33,34) table(data.frame(a=a,b=b)) # b # a 32 33 34 # 59 7 0 0 # 60 1 9 0 # 61 0 10 0 # 62 0 0 1 T <- t(as.matrix(table(data.frame(a=a,b=b)))) L <- c(table(data.frame(a=a,b=b))) B <- as.integer(rep(rownames(T),each=ncol(T))) A <- as.integer(rep(colnames(T),nrow(T))) cbind(A,B,L)[L>0,] # A B L # [1,] 59 32 7 # [2,] 60 32 1 # [3,] 60 33 9 # [4,] 61 33 10 # [5,] 62 34 1 Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 17-Sep-10 Time: 14:15:03 ------------------------------ XFMail ------------------------------
Alaios, Try as.data.frame(table(x[,1], x[,2)) where x is your matrix. HTH, Jorge On Fri, Sep 17, 2010 at 8:19 AM, 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) > > Best Regards > Alex > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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]]
Seemingly Similar Threads
- replace repeated id in a pedigree list
- re: Btrfs: fix num_workers_starting bug and other bugs in async thread
- *** buffer overflow detected *** accessing invalid FD in libguestfs
- unionfs related patch
- [Codegen bug in LLVM 3.8?] br following `fcmp une` is present in ll, absent in asm