I have the following problem: I have some string with numbers like k. I want to have a table like the function table() gives. However I am not able to call the first row, the 1, 2, 3, 5, 6 or 9. I tried to do that by a data.frame, but that doesn't seem to work either. The levels keep bothering me. This is an example of the code: k<-c(1,1,1,1,1,3,5,6,2,1,3,9,2,3,1,1,1)> table(k)k 1 2 3 5 6 9 9 2 3 1 1 1> x<-table(k) > > dim(x)[1] 6> > x[1] #But i only want the one1 9> > x<-data.frame(x) > > x[1,1] #You are not allowed to use this one for example 3*x[1,1] is > impossible[1] 1 Levels: 1 2 3 5 6 9>I hope anyone has an idea of using the table function without this inconvenience. I thought about writing a counter myself, but that seems complicated. Because I have to examine very large examples later on, I don't want to slow the calculations down if possible. Thanks for your help in advance. Frederique -- View this message in context: http://r.789695.n4.nabble.com/table-reading-problem-tp3381174p3381174.html Sent from the R help mailing list archive at Nabble.com.
It isn't entirely clear to me what you want. table() can function with many kinds of data, not just integers, so it returns a vector with names. For your case, with integer classes, you seem to possibly want:> x <- table(k) > x <- rbind(as.numeric(names(x)), as.numeric(x)) > x[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 2 3 5 6 9 [2,] 9 2 3 1 1 1 but a more general approach is:> x <- table(k) > x[1]1 9> names(x[1])[1] "1"> as.numeric(names(x[1]))[1] 1 Sarah On Wed, Mar 16, 2011 at 5:20 AM, fre <fre_stamlid at hotmail.com> wrote:> I have the following problem: > > I have some string with numbers like k. I want to have a table like the > function table() gives. However I am not able to call the first row, the 1, > 2, 3, 5, 6 or 9. I tried to do that by a data.frame, but that doesn't seem > to work either. The levels keep bothering me. > > This is an example of the code: > > k<-c(1,1,1,1,1,3,5,6,2,1,3,9,2,3,1,1,1) >> table(k) > k > 1 2 3 5 6 9 > 9 2 3 1 1 1 >> x<-table(k) >> >> dim(x) > [1] 6 >> >> x[1] #But i only want the one > 1 > 9 >> >> x<-data.frame(x) >> >> x[1,1] #You are not allowed to use this one for example 3*x[1,1] is >> impossible > [1] 1 > Levels: 1 2 3 5 6 9 >> > > I hope anyone has an idea of using the table function without this > inconvenience. I thought about writing a counter myself, but that seems > complicated. > Because I have to examine very large examples later on, I don't want to slow > the calculations down if possible. > > Thanks for your help in advance. > > Frederique >-- Sarah Goslee http://www.functionaldiversity.org
On 16/03/11 09:20, fre wrote:> I have the following problem: > > I have some string with numbers like k. I want to have a table like the > function table() gives. However I am not able to call the first row, the 1, > 2, 3, 5, 6 or 9. I tried to do that by a data.frame, but that doesn't seemThe first row, as you call it, is accessible as the vector dimnames(x)$k (or as.numeric(dimnames(x)$k) if you need numbers, not strings).> to work either. The levels keep bothering me. > > This is an example of the code: > > k<-c(1,1,1,1,1,3,5,6,2,1,3,9,2,3,1,1,1) >> table(k) > k > 1 2 3 5 6 9 > 9 2 3 1 1 1Maybe something like matrix(c(as.integer(dimnames(x)$k), x), nrow=2, byrow=TRUE) is what you are looking for? Hope this helps Allan>> x<-table(k) >> >> dim(x) > [1] 6 >> x[1] #But i only want the one > 1 > 9 >> x<-data.frame(x) >> >> x[1,1] #You are not allowed to use this one for example 3*x[1,1] is >> impossible > [1] 1 > Levels: 1 2 3 5 6 9 > I hope anyone has an idea of using the table function without this > inconvenience. I thought about writing a counter myself, but that seems > complicated. > Because I have to examine very large examples later on, I don't want to slow > the calculations down if possible. > > Thanks for your help in advance. > > Frederique > > -- > View this message in context: http://r.789695.n4.nabble.com/table-reading-problem-tp3381174p3381174.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.
On 03/16/2011 08:20 PM, fre wrote:> I have the following problem: > > I have some string with numbers like k. I want to have a table like the > function table() gives. However I am not able to call the first row, the 1, > 2, 3, 5, 6 or 9. I tried to do that by a data.frame, but that doesn't seem > to work either. The levels keep bothering me. > > This is an example of the code: > > k<-c(1,1,1,1,1,3,5,6,2,1,3,9,2,3,1,1,1) >> table(k) > k > 1 2 3 5 6 9 > 9 2 3 1 1 1 >> x<-table(k) >> >> dim(x) > [1] 6 >> >> x[1] #But i only want the one > 1 > 9 >> >> x<-data.frame(x) >> >> x[1,1] #You are not allowed to use this one for example 3*x[1,1] is >> impossible > [1] 1 > Levels: 1 2 3 5 6 9 >> > > I hope anyone has an idea of using the table function without this > inconvenience. I thought about writing a counter myself, but that seems > complicated. > Because I have to examine very large examples later on, I don't want to slow > the calculations down if possible. > > Thanks for your help in advance. >Hi Frederique, It seems to me that you want the names of the table rather than the counts, as Sarah has already suggested. So: as.numeric(names(x)) should give you what you want. However, if all you want to do is determine the unique elements of k, then: unique(k) might be what you are seeking. Jim
Sender: r-help-bounces at r-project.org On-Behalf-Of: jim at bitwrit.com.au Subject: Re: [R] table() reading problem Message-Id: <4D81C14A.2000402 at bitwrit.com.au> Recipient: killian.doorley at barclayscapital.com _______________________________________________ e at 1 Churchill Place, London, E14 5HP. This email may relate to or be sent from other members of the Barclays Group. _______________________________________________ -------------- next part -------------- An embedded message was scrubbed... From: Jim Lemon <jim at bitwrit.com.au> Subject: Re: [R] table() reading problem Date: Thu, 17 Mar 2011 19:07:38 +1100 Size: 5718 URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110317/956e48ce/attachment.mht>