Hi How do I get the output from table() in matrix form? If I have R> table(c(1,1,1,1,2,20)) 1 2 20 4 1 1 I want [,1] [,2] [,3] [1,] 1 2 20 [2,] 4 1 1 The problem is that names(table) is a vector of characters and I need the numeric values. I am using R> rbind(as.integer(names(x)),x) I thought tabulate() might be better as it takes an integer-valued vector, but it isn't quite right because the default bins are 1:20 and I don't want the zeroes. The following is a little clunky: R> x <- rbind(1:20,tabulate(c(1,1,1,1,2,20))) R> x[,x[2,]>0] [,1] [,2] [,3] [1,] 1 2 20 [2,] 4 1 1 Is there a better way? It seems inelegant to coerce a character vector back to integers, but OTOH it's wasteful to have 20 bins when I only need 3. My real application would have maybe a dozen distinct (prime) integers in the range 2 up to about 1e4. -- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
Hi Robin, does this help: x <- table(c(1,1,1,1,2,20)) matrix(c(as.numeric(names(x)), x), ncol=length(x), byrow=TRUE, dimnames=NULL) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Robin Hankin" <r.hankin at soc.soton.ac.uk> To: <R-help at stat.math.ethz.ch> Sent: Wednesday, January 05, 2005 1:18 PM Subject: [R] output from table() in matrix form> Hi > > How do I get the output from table() in matrix form? > > If I have > > R> table(c(1,1,1,1,2,20)) > > 1 2 20 > 4 1 1 > > I want > > [,1] [,2] [,3] > [1,] 1 2 20 > [2,] 4 1 1 > > > The problem is that names(table) is a vector of characters and I > need the numeric values. > > I am using > > R> rbind(as.integer(names(x)),x) > > > > > I thought tabulate() might be better as it takes an integer-valued > vector, but it isn't > quite right because the default bins are 1:20 and I don't want the > zeroes. > > The following is a little clunky: > > R> x <- rbind(1:20,tabulate(c(1,1,1,1,2,20))) > R> x[,x[2,]>0] > [,1] [,2] [,3] > [1,] 1 2 20 > [2,] 4 1 1 > > > Is there a better way? It seems inelegant to coerce a character > vector back to integers, > but OTOH it's wasteful to have 20 bins when I only need 3. My real > application would have > maybe a dozen distinct (prime) integers in the range 2 up to about > 1e4. > > > > > > -- > Robin Hankin > Uncertainty Analyst > Southampton Oceanography Centre > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > 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 >
[This email is either empty or too large to be displayed at this time]
You probably want something like:> t1 <- table( x ) > t1x 1 2 20 3 2 1> t2 <- rbind( as.numeric( names( t1 ) ), t1 ) > t21 2 20 1 2 20 t1 3 2 1> dimnames( t2 ) <- NULL > t2[,1] [,2] [,3] [1,] 1 2 20 [2,] 3 2 1>Bendix ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc at steno.dk www.biostat.ku.dk/~bxc ----------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Robin Hankin > Sent: Wednesday, January 05, 2005 1:18 PM > To: R-help at stat.math.ethz.ch > Subject: [R] output from table() in matrix form > > > Hi > > How do I get the output from table() in matrix form? > > If I have > > R> table(c(1,1,1,1,2,20)) > > 1 2 20 > 4 1 1 > > I want > > [,1] [,2] [,3] > [1,] 1 2 20 > [2,] 4 1 1 > > > The problem is that names(table) is a vector of characters and I need > the numeric values. > > I am using > > R> rbind(as.integer(names(x)),x) > > > > > I thought tabulate() might be better as it takes an integer-valued > vector, but it isn't > quite right because the default bins are 1:20 and I don't want the > zeroes. > > The following is a little clunky: > > R> x <- rbind(1:20,tabulate(c(1,1,1,1,2,20))) > R> x[,x[2,]>0] > [,1] [,2] [,3] > [1,] 1 2 20 > [2,] 4 1 1 > > > Is there a better way? It seems inelegant to coerce a character > vector back to integers, > but OTOH it's wasteful to have 20 bins when I only need 3. My real > application would have > maybe a dozen distinct (prime) integers in the range 2 up to > about 1e4. > > > > > > -- > Robin Hankin > Uncertainty Analyst > Southampton Oceanography Centre > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > 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 >
table works the way it does because it applies to *factors*, so the names are the factor levels of the argument after conversion. So if anything is wasteful, that is. How about using the guts of factor and table, via xx <- unique(x) rbind(vals=xx, cnts=tabulate(match(x, xx))) ? On Wed, 5 Jan 2005, Robin Hankin wrote:> Hi > > How do I get the output from table() in matrix form? > > If I have > > R> table(c(1,1,1,1,2,20)) > > 1 2 20 > 4 1 1 > > I want > > [,1] [,2] [,3] > [1,] 1 2 20 > [2,] 4 1 1 > > > The problem is that names(table) is a vector of characters and I need the > numeric values. > > I am using > > R> rbind(as.integer(names(x)),x) > > > > > I thought tabulate() might be better as it takes an integer-valued vector, > but it isn't > quite right because the default bins are 1:20 and I don't want the zeroes. > > The following is a little clunky: > > R> x <- rbind(1:20,tabulate(c(1,1,1,1,2,20))) > R> x[,x[2,]>0] > [,1] [,2] [,3] > [1,] 1 2 20 > [2,] 4 1 1 > > > Is there a better way? It seems inelegant to coerce a character vector back > to integers, > but OTOH it's wasteful to have 20 bins when I only need 3. My real > application would have > maybe a dozen distinct (prime) integers in the range 2 up to about 1e4. > > > > > > -- > Robin Hankin > Uncertainty Analyst > Southampton Oceanography Centre > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > 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 >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595