Take the 2-minute tour ×  Is this a bug in R or does it make
sense?  ## works aa <- matrix(nrow=1,ncol=2) dimnames(aa)[[2]]
<- c("a","b") dimnames(aa)[[1]] <- c("c")  ## does not work bb
<- matrix(nrow=1,ncol=2) dimnames(bb)[[1]] <- c("c") Error in
dimnames(bb)[[1]] <- c("c") : 'dimnames' must be a list  Thanks
for explanations !  share|improve this question add comment  3
Answers  up vote 7 down vote accepted  This is expected
behavior. When you assign to an index of an element of an
object, R will create the element if it doesn't exist. In your
example "dimnames" doesn't exist in aa, so R tries to create
"dimnames" based on what you're assigning to it. Consider
assigning elements named "a", "b", and "c" of a list:  > L <-
list() > L$a[[1]] <- 5 > L$b[[3]] <- "foo" > L$c[[4]] <-
c(1,2,3) > class(L$a) [1] "numeric" > class(L$b) [1] "character"
> class(L$c) [1] "list"  Now the problem with saying L$a[[1]] <-
anything is that L$a doesn't exist yet. What happens when an
element doesn't exist is that R just creat