I want to create a vecor with frequencies. I have tried this: a <- c(1,1,1,1,2,3,4,5,5) b <- table(a) print (b[1]) which results in:> print (b[1])1 4 The only thing I want is the 4. So this seems obvious: print (b[1,2]) but it does not work: Error in b[1, 2] : incorrect number of dimensions How do I get a vector or how do I refer to the "4" without getting the "1" label as well? -- View this message in context: http://www.nabble.com/Frequency-vector-tp18939882p18939882.html Sent from the R help mailing list archive at Nabble.com.
how about: a <- c(1,1,1,1,2,3,4,5,5) b <- as.data.frame(table(a)) b a Freq 1 1 4 2 2 1 3 3 1 4 4 1 5 5 2 which you can then select the bits you want from. David dennis11 wrote:> > I want to create a vecor with frequencies. > > I have tried this: > > a <- c(1,1,1,1,2,3,4,5,5) > b <- table(a) > print (b[1]) > > which results in: >> print (b[1]) > 1 > 4 > > The only thing I want is the 4. > > So this seems obvious: > print (b[1,2]) > > but it does not work: > Error in b[1, 2] : incorrect number of dimensions > > How do I get a vector or how do I refer to the "4" without getting the "1" > label as well? >----- Institute for Transport Studies University of Leeds -- View this message in context: http://www.nabble.com/Frequency-vector-tp18939882p18940830.html Sent from the R help mailing list archive at Nabble.com.
On Tue, 2008-08-12 at 01:21 -0700, dennis11 wrote:> I want to create a vecor with frequencies. > > I have tried this: > > a <- c(1,1,1,1,2,3,4,5,5) > b <- table(a) > print (b[1]) > > which results in: > > print (b[1]) > 1 > 4 > The only thing I want is the 4.Isn't this one if the situations where you actually want b[[1]]?> So this seems obvious: > print (b[1,2]) > > but it does not work: > Error in b[1, 2] : incorrect number of dimensions > > How do I get a vector or how do I refer to the "4" without getting the "1" > label as well?-- Michael R. Head <burner at suppressingfire.org> http://www.cs.binghamton.edu/~mike/
On Tue, Aug 12, 2008 at 01:21:29AM -0700, dennis11 wrote:> > I want to create a vecor with frequencies. > > I have tried this: > > a <- c(1,1,1,1,2,3,4,5,5) > b <- table(a) > print (b[1]) > > which results in: > > print (b[1]) > 1 > 4 > > The only thing I want is the 4. > > So this seems obvious: > print (b[1,2])No! The "1" is just a label. You're not looking at a matrix. (BTW, I think you meant b[2,1]). First I would say don't get rid of the "1" label unless you need to. It's just a label telling you what the count is referring to, and it wouldn't be there if there weren't a good reason for it. It won't interfere with any numeric calculations you do, e.g.> b[1] * 21 8 But if you really want to extract the integer counts from an object of class "table" you could do> as.vector(b)[1] 4 1 1 1 2 Remember that if an object is not behaving as you would expect, use str() and class() to see what you've really got:> class(b)[1] "table"> str(b)'table' int [, 1:5] 4 1 1 1 2 - attr(*, "dimnames")=List of 1 ..$ a: chr [1:5] "1" "2" "3" "4" ... Dan> > but it does not work: > Error in b[1, 2] : incorrect number of dimensions > > How do I get a vector or how do I refer to the "4" without getting the "1" > label as well?> -- > View this message in context: http://www.nabble.com/Frequency-vector-tp18939882p18939882.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.-- www.stats.ox.ac.uk/~davison
Hi Dennis, you could get the result using 2 approachs: 1) a <- c(1,1,1,1,2,3,4,5,5) b <- table(a)> print (b[1])1 4 Now, print (b[[1]])> 4> rownames(b)[1] "1" "2" "3" "4" "5" c =rownames(b) print(c[1]) 1 2)> b= tabulate(a) > b[1] 4 1 1 1 2> print(b[1])[1] 4 Emir Toktar> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of dennis11 > Sent: Tuesday, August 12, 2008 5:21 AM > To: r-help at r-project.org > Subject: [R] Frequency vector > > > I want to create a vecor with frequencies. > > I have tried this: > > a <- c(1,1,1,1,2,3,4,5,5) > b <- table(a) > print (b[1]) > > which results in: > > print (b[1]) > 1 > 4 > > The only thing I want is the 4. > > So this seems obvious: > print (b[1,2]) > > but it does not work: > Error in b[1, 2] : incorrect number of dimensions > > How do I get a vector or how do I refer to the "4" without > getting the "1" > label as well? > -- > View this message in context: > http://www.nabble.com/Frequency-vector-tp18939882p18939882.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.