[R] count frequency
(Ted Harding)
Ted.Harding at manchester.ac.uk
Fri Sep 17 15:15:06 CEST 2010
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
> Alex
Does 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 ------------------------------
More information about the R-help
mailing list