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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With