Tom Cohen
2007-Aug-10 12:01 UTC
[R] help with counting how many times each value occur in each column
Dear list, I have the following dataset and want to know how many times each value occur in each column. >data [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] -100 -100 -100 0 0 0 0 0 0 -100 [2,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [3,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [4,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [5,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -50 [6,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [7,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [8,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [9,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [10,] -100 -100 -100 -50 -100 -100 -100 -100 -100 -100 [11,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [12,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [13,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [14,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [15,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [16,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [17,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [18,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 [19,] -100 -100 -100 0 0 0 0 0 0 -100 [20,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 The result matrix should look like -100 0 -50 [1] 20 [2] 20 [3] 20 [4] 17 [5] 18 [6] 18 [7] 18 and so on [8] [9] [10] How can I do this in R ? Thanks alot for your help, Tom --------------------------------- Jämför pris på flygbiljetter och hotellrum: http://shopping.yahoo.se/c-169901-resor-biljetter.html [[alternative HTML version deleted]]
Petr PIKAL
2007-Aug-10 12:42 UTC
[R] Odp: help with counting how many times each value occur in each column
Hi> mat<-sample(c(-50,0,-100), 100,replace=T) > dim(mat)<-c(10,10) > mat[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 0 0 0 -50 0 0 0 0 0 [2,] -100 -100 -50 -50 0 0 -100 -50 -100 -50 [3,] 0 -50 -100 -100 0 -50 -100 0 0 -100 [4,] 0 -100 0 -50 -100 -100 -50 -50 0 -100 [5,] -50 -50 0 0 0 -100 -100 -100 0 -100 [6,] 0 0 -50 -50 0 0 -100 -100 -50 -100 [7,] -100 -100 -100 -50 -100 0 -100 -100 0 -100 [8,] -100 0 0 0 0 -100 0 -100 0 -100 [9,] -100 0 -50 -100 -50 0 0 -50 0 -100 [10,] -50 -100 0 0 -50 -50 -50 -50 -100 -100> apply(mat, 2, table)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] -100 4 4 2 2 2 3 5 4 2 8 -50 2 2 3 4 3 2 2 4 1 1 0 4 4 5 4 5 5 3 2 7 1 Transposing and ordering columns is up to you. Regards Petr r-help-bounces at stat.math.ethz.ch napsal dne 10.08.2007 14:01:44:> Dear list, > I have the following dataset and want to know how many times eachvalue> occur in each column. > >data > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] -100 -100 -100 0 0 0 0 0 0 -100 > [2,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [3,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [4,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [5,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -50 > [6,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [7,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [8,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [9,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [10,] -100 -100 -100 -50 -100 -100 -100 -100 -100 -100 > [11,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [12,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [13,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [14,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [15,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [16,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [17,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [18,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [19,] -100 -100 -100 0 0 0 0 0 0 -100 > [20,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > The result matrix should look like > -100 0 -50 > [1] 20 > [2] 20 > [3] 20 > [4] 17 > [5] 18 > [6] 18 > [7] 18 and so on > [8] > [9] > [10] > > How can I do this in R ? > Thanks alot for your help, > Tom > > > --------------------------------- > > J?mf?r pris p? flygbiljetter och hotellrum:http://shopping.yahoo.se/c-169901-> resor-biljetter.html > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Gabor Grothendieck
2007-Aug-10 13:23 UTC
[R] help with counting how many times each value occur in each column
Try this where we have constructed the example to illustrate that it does handle the case where not all values are in each column: mat <- matrix(rep(1:6, each = 4), 6) table(col(mat), mat) On 8/10/07, Tom Cohen <tom.cohen78 at yahoo.se> wrote:> Dear list, > I have the following dataset and want to know how many times each value occur in each column. > >data > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] -100 -100 -100 0 0 0 0 0 0 -100 > [2,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [3,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [4,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [5,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -50 > [6,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [7,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [8,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [9,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [10,] -100 -100 -100 -50 -100 -100 -100 -100 -100 -100 > [11,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [12,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [13,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [14,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [15,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [16,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [17,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [18,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [19,] -100 -100 -100 0 0 0 0 0 0 -100 > [20,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > The result matrix should look like > -100 0 -50 > [1] 20 > [2] 20 > [3] 20 > [4] 17 > [5] 18 > [6] 18 > [7] 18 and so on > [8] > [9] > [10] > > How can I do this in R ? > Thanks alot for your help, > Tom > > > --------------------------------- > > J?mf?r pris p? flygbiljetter och hotellrum: http://shopping.yahoo.se/c-169901-resor-biljetter.html > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >
François Pinard
2007-Aug-10 14:47 UTC
[R] help with counting how many times each value occur in each column
[Tom Cohen]> I have the following dataset and want to know how many times each value occur in each column.> >data > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] -100 -100 -100 0 0 0 0 0 0 -100 > [2,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [3,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [4,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [5,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -50 > [6,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [7,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [8,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > [9,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[10,] -100 -100 -100 -50 -100 -100 -100 -100 -100 -100 >[11,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[12,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[13,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[14,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[15,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[16,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[17,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[18,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 >[19,] -100 -100 -100 0 0 0 0 0 0 -100 >[20,] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 > The result matrix should look like > -100 0 -50 >[1] 20 >[2] 20 >[3] 20 >[4] 17 >[5] 18 >[6] 18 >[7] 18 and so on >[8] >[9] >[10]Presuming that "data" is a matrix, one could try a sequence like this: dataf <- factor(data) dim(dataf) <- dim(data) result <- t(apply(dataf, 2, tabulate, nlevels(dataf))) colnames(result) <- levels(dataf) result If you want the columns sorted, you might decide the order of the levels on the "factor()" call, or explicitly reorder columns afterwards. -- Fran?ois Pinard http://pinard.progiciels-bpi.ca
Possibly Parallel Threads
- ggplot2: labels points with different colors or idnumbers
- adding the mean and standard deviation to boxplots
- Reorder the x-axis using lattice
- help with making a function of scatter plot with multiple variables
- help with reshaping data into long format (correct question)