Re: [R] Adding rownames with different lengths to a table

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sun, 27 Apr 2008 22:37:23 +0000 (UTC)

filip rendel <filip61_at_hotmail.com> wrote in news:BAY129-W482E25FE18F0FBDA8C7346B2DF0_at_phx.gbl:

>
> Hello! I would like to add names to a table. The table presents
> median values, numbers of values (n) and percentage(%) for a given
> agegroup. Thus I would like to add a label above these three
> variables for each category. But if I try to use colnames I get a
> message telling me that the number of labels must be equal to the
> number of columns in the table. I hope someone could understand what
> I mean!
>
> [[alternative HTML version deleted]]

You can use indexing and I had better luck with names(), e.g.:

names(df)[5] <- "var.median"

----example
> table(rpois(100,5))

 1 2 3 4 5 6 7 8 9 10 11
 6 6 5 21 17 18 12 5 7 2 1
> xp <- table(rpois(100,5))
> class(xp)

[1] "table"
> names(xp)[5] <- "mode"
> xp

   1 2 3 4 mode 6 7 8 9 10 11    5 10 15 16 19 12 8 6 6 2 1

#--colnames appears not to work for this class---
> colnames(xp)[4] <- "four"

Error in dn[[2]] : subscript out of bounds

I would have thought that a table object was a "matrix-like object", but I may be confusing the R definitions of arrays and matrices.

-- 
David Winsemius

______________________________________________
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.
Received on Sun 27 Apr 2008 - 22:52:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Apr 2008 - 08:30:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive