[R] Adding rownames with different lengths to a table

0 views
Skip to first unread message

filip rendel

unread,
Apr 27, 2008, 11:36:17 AM4/27/08
to r-h...@r-project.org

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]]

______________________________________________
R-h...@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.

David Winsemius

unread,
Apr 27, 2008, 6:37:23 PM4/27/08
to r-h...@stat.math.ethz.ch
filip rendel <fil...@hotmail.com> wrote in
news:BAY129-W482E25FE1...@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

David Winsemius

unread,
Apr 28, 2008, 12:27:39 AM4/28/08
to r-h...@stat.math.ethz.ch
David Winsemius <dwins...@comcast.net> wrote in
news:Xns9A8DBD1CB2FF...@80.91.229.13:

Prompted by an offlist email suggesting that tables only had one named
dimension, I have looked at this again. I was wrong about tables not
being acceptable arguments to colnames(). The error was in assuming
that the above table, "xp", had one row and 11 columns. Instead, it's
11 rows and 1 column. I was misled by the display orientation.

rownames(xp)[5:8] <- c("five", "six", "seven", "eight")
# no error
> xp
1 2 3 4 five six seven eight 9 10 11

5 10 15 16 19 12 8 6 6 2 1

And tables can have both row and column names
airtb <- with(airquality, table(cut(Temp, quantile(Temp)), Month))

> str(airtb)
'table' int [1:4, 1:5] 24 5 1 0 3 15 7 5 0 2 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:4] "(56,72]" "(72,79]" "(79,85]" "(85,97]"
..$ Month: chr [1:5] "5" "6" "7" "8" ...

airtb
Month


5 6 7 8 9

(56,72] 24 3 0 1 10
(72,79] 5 15 2 9 10
(79,85] 1 7 19 7 5
(85,97] 0 5 10 14 5
> colnames(airtb)
[1] "5" "6" "7" "8" "9"

Reply all
Reply to author
Forward
0 new messages