[R] Counting Frequencies in Data Frame
William Dunlap
wdunlap at tibco.com
Tue May 18 17:33:50 CEST 2010
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of M.Ribeiro
> Sent: Tuesday, May 18, 2010 7:13 AM
> To: r-help at r-project.org
> Subject: [R] Counting Frequencies in Data Frame
>
>
> Hi,
> I am sure there is an easy way to do it, but I can't find it.
> I have a data frame that has 15 columns and 7000 rows.
>
> The only values inside the data.frame are "aa", "ab", "bb" as
> you can see an
> example bellow.
>
> 1 2 3
> 1 aa ab ab
> 2 ab ab ab
> 3 aa aa aa
> 4 bb bb bb
>
> What I would like to do, is to generate a vector (or another
> data.frame)
> with 7000 rows, and 3 columns. In the first column, the
> information about
> how many aa, the second about how many ab, and the third
> about how many bb
> are there in each line
> aa ab bb
> 1 1 2 0
> 2 0 2 0
--> 3? <--
> 3 3 0 0
> 4 0 0 3
You could make a "table" (a sort of "matrix") of the
data entries and their row numbers:
> tmp <- read.table(header=TRUE, check.names=FALSE, textConnection("
+ 1 2 3
+ 1 aa ab ab
+ 2 ab ab ab
+ 3 aa aa aa
+ 4 bb bb bb
+ "))
> table(row(tmp), as.matrix(tmp))
aa ab bb
1 1 2 0
2 0 3 0
3 3 0 0
4 0 0 3
The as.matrix() is needed to force all the columns of the
data.frame into one vector.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
>
> Thank you very much
> Cheers
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Counting-Frequencies-in-Data-Fra
me-tp2221342p2221342.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list