[R] FW: Transposing the output of 'table'

peter dalgaard pdalgd at gmail.com
Tue Oct 8 00:11:46 CEST 2013


On Oct 6, 2013, at 20:25 , Andrea Lamont wrote:

> If t(table(OBJECT)) does not work, does:
> u<-as.matrix(table(OBJ))
> t(u)
> 
> -i.e. use matrix operations?


Please try before posting...

table() results in a 1d table, i.e. a vector; t() of a vector is a row matrix, so it still displays horisontally. To get a column matrix, it works to transpose twice, but as.matrix works too, as others have already pointed out. I think my favorite is to use cbind(), which conveniently also allows you to set a heading:

> cbind(Freq=table(airquality$Month))
  Freq
5   31
6   30
7   31
8   31
9   30

- Peter D.

> 
> 
> 
> 
> On Sun, Oct 6, 2013 at 1:47 PM, LAMONT, ANDREA <LAMONTA at mailbox.sc.edu>wrote:
> 
>> 
>> 
>> 
>> ________________________________________
>> From: r-help-bounces at r-project.orgOn Behalf OfBerend Hasselman
>> Sent: Sunday, October 06, 2013 1:44:30 PM (UTC-05:00) Eastern Time (US &
>> Canada)
>> To: Dennis Fisher
>> Cc: r-help at stat.math.ethz.ch
>> Subject: Re: [R] Transposing the output of 'table'
>> 
>> On 06-10-2013, at 19:30, Dennis Fisher <fisher at plessthan.com> wrote:
>> 
>>> 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?
>> 
>> 
>> Have you tried t(table(OBJECT)) ?
>> 
>> Berend
>> 
>> ______________________________________________
>> 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.
>> 
> 
> 
> 
> -- 
> Andrea Lamont, MA
> Clinical-Community Psychology
> University of South Carolina
> Barnwell College
> Columbia, SC 29208
> 
> Please consider the environment before printing this email.
> 
> CONFIDENTIAL: This transmission is intended for the use of the
> individual(s) or entity to which it is addressed, and may contain
> information that is privileged, confidential, and exempt from disclosure
> under applicable law. Should the reader of this message not be the intended
> recipient(s), you are hereby notified that any dissemination, distribution,
> or copying of this communication is strictly prohibited.  If you are not
> the intended recipient, please contact the sender by reply email and
> destroy/delete all copies of the original message.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list