5

It seems that when accessing tables with 1 column, the action of accessing throws away the column information. This information is kept if there is more than 1 column.

E.g.

With 1 item in the table:

> example1 <- data.frame( items = c("A","B","B","C","C","C","C")
+                       , time = ISOdate(2222,1,1) )
> table1 <- table(example1)
> table1
     time
items 2222-01-01 12:00:00
    A                   1
    B                   2
    C                   4

> barplot(table1, legend=T)
> table1.ordered <- table1[c(3,2,1),]   # reorder
> table1.ordered
C B A 
4 2 1 
> barplot(table1.ordered, legend=T) # time column thrown away

Now with 2 items in table: (added onto example1 in this example)

> example2 <- rbind(example1 , data.frame(items = NA, time = ISOdate(3333,1,1)) )
> example2
  items                time
1     A 2222-01-01 12:00:00
2     B 2222-01-01 12:00:00
3     B 2222-01-01 12:00:00
4     C 2222-01-01 12:00:00
5     C 2222-01-01 12:00:00
6     C 2222-01-01 12:00:00
7     C 2222-01-01 12:00:00
8  <NA> 3333-01-01 12:00:00

> table2 <- table(example2)
> table2
     time
items 2222-01-01 12:00:00 3333-01-01 12:00:00
    A                   1                   0
    B                   2                   0
    C                   4                   0
> table2.ordered <- table2[c(3,2,1),]    #Again, reorder
> table2.ordered
     time
items 2222-01-01 12:00:00 3333-01-01 12:00:00
    C                   4                   0
    B                   2                   0
    A                   1                   0
> barplot(table2.ordered, legend=T)    #displays how I expected
Terry
  • 199
  • 3
  • 8

1 Answers1

8

The old default drop=TRUE problem...

What you get with the reordering, is that the table is translated to a vector. If you look at the structure of table1, you see that it is in fact a matrix with one column (the counts), and both time and items are names of the dimensions. These are attributes, and not part of the matrix.

Using the reordering also uses the default drop=TRUE from the [ function. So that matrix with only one column is translated to a vector. Subsequently, the name attribute for the columns (time) is dropped as well.

You can avoid that by:

table1.ordered <- table1[c(3,2,1),,drop=FALSE]   # reorder
table1.ordered

     time
items 2222-01-01 12:00:00
    C                   4
    B                   2
    A                   1

The argument drop=TRUE lets the [ function drop dimensions that are 1. In this case, the matrix is simplified to a vector. If you don't want that to happen, you have to use drop=FALSE. Another example :

> X <- matrix(1:4,ncol=2)
> X[,1]
[1] 1 2
> X[,1,drop=FALSE]
     [,1]
[1,]    1
[2,]    2
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • Thanks for that answer. I'd been losing my mind and missed my deadline over this issue. This is a bit of a n00b trap. The other most non-obvious tip I found was [barplot uses different coordinates](http://stackoverflow.com/questions/6343776/barplot-changing-x-axe-and-adding-line/6348531#6348531) – Terry Oct 29 '11 at 14:27