Dear R users, I would like to great a frequency table from raw data and then access the classes/bins and their respective frequencies separately. Here the code to create the frequency tables: x1 <- c(1,5,1,1,2,2,3,4,5,3,2,3,6,4,3,8) t1 <- table(x1) print(t1[1]) Its easy to plot this, but how do I actually access the frequencies alone and the bins alone? Basically I am looking to get: bins <- c(1, 2, 3, 4, 5, 6, 8) freq <- c(3, 3, 4, 2, 2, 1, 1) When running print(t1[1]) I only get one pair. It seems to be organized that way. Is there a better way? Perhaps 'table' is not the right approach? Thanks a lot, Ralf
Mike Rennie
2010-Sep-22 18:50 UTC
[R] Extracting bins and frequencies from frequency table
Hi Ralf try hist() obl<-hist(x1, plot=FALSE) it returns midpoints and their respective frequencies. You can specify the breakpoints as well. ?hist for details. Mike On Wed, Sep 22, 2010 at 1:44 PM, Ralf B <ralf.bierig@gmail.com> wrote:> Dear R users, > > I would like to great a frequency table from raw data and then access > the classes/bins and > their respective frequencies separately. Here the code to create the > frequency tables: > > > x1 <- c(1,5,1,1,2,2,3,4,5,3,2,3,6,4,3,8) > t1 <- table(x1) > print(t1[1]) > > Its easy to plot this, but how do I actually access the frequencies > alone and the bins alone? > Basically I am looking to get: > > bins <- c(1, 2, 3, 4, 5, 6, 8) > freq <- c(3, 3, 4, 2, 2, 1, 1) > > When running > > print(t1[1]) > > I only get one pair. It seems to be organized that way. Is there a > better way? Perhaps 'table' is not the right approach? > > Thanks a lot, > Ralf > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Phil Spector
2010-Sep-22 18:54 UTC
[R] Extracting bins and frequencies from frequency table
Ralf - Try bins = as.numeric(names(t1)) freqs = as.vector(t1) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 22 Sep 2010, Ralf B wrote:> Dear R users, > > I would like to great a frequency table from raw data and then access > the classes/bins and > their respective frequencies separately. Here the code to create the > frequency tables: > > > x1 <- c(1,5,1,1,2,2,3,4,5,3,2,3,6,4,3,8) > t1 <- table(x1) > print(t1[1]) > > Its easy to plot this, but how do I actually access the frequencies > alone and the bins alone? > Basically I am looking to get: > > bins <- c(1, 2, 3, 4, 5, 6, 8) > freq <- c(3, 3, 4, 2, 2, 1, 1) > > When running > > print(t1[1]) > > I only get one pair. It seems to be organized that way. Is there a > better way? Perhaps 'table' is not the right approach? > > Thanks a lot, > Ralf > > ______________________________________________ > 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. >
Henrique Dallazuanna
2010-Sep-22 18:56 UTC
[R] Extracting bins and frequencies from frequency table
Try this: as.data.frame(table(x1)) On Wed, Sep 22, 2010 at 3:44 PM, Ralf B <ralf.bierig@gmail.com> wrote:> Dear R users, > > I would like to great a frequency table from raw data and then access > the classes/bins and > their respective frequencies separately. Here the code to create the > frequency tables: > > > x1 <- c(1,5,1,1,2,2,3,4,5,3,2,3,6,4,3,8) > t1 <- table(x1) > print(t1[1]) > > Its easy to plot this, but how do I actually access the frequencies > alone and the bins alone? > Basically I am looking to get: > > bins <- c(1, 2, 3, 4, 5, 6, 8) > freq <- c(3, 3, 4, 2, 2, 1, 1) > > When running > > print(t1[1]) > > I only get one pair. It seems to be organized that way. Is there a > better way? Perhaps 'table' is not the right approach? > > Thanks a lot, > Ralf > > ______________________________________________ > 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]]