[R] Transposing the output of 'table'

Law, Jason Jason.Law at portlandoregon.gov
Mon Oct 7 23:41:10 CEST 2013


If you want a dataframe rather than a matrix, I often use the as.data.frame method for table objects.  See ?table for the documentation.  You can even nicely name the dimensions and frequency.

OBJECT <- sample(4, 20, TRUE)

as.data.frame(table(var1 = OBJECT), responseName = 'frequency')

Jason

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Dennis Fisher
Sent: Sunday, October 06, 2013 10:31 AM
To: r-help at stat.math.ethz.ch
Subject: [R] Transposing the output of 'table'

R 3.0.1
OS X

Colleagues,

If I execute the command:
	table(OBJECT)
the output might look like:
  1   2 
 25 336 

I would like it to appear as:
	1	25
	2	336

I can accomplish this with:
	TABLE	<- table(OBJECT)
	data.frame(names(TABLE), as.numeric(TABLE))

However, I bet that a more clever approach exists?  Any takers?

Dennis


Dennis Fisher MD
P < (The "P Less Than" Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-866-PLessThan (1-866-753-7784)
www.PLessThan.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