[R] boundary situation that caught me by surprise

RICHARD M. HEIBERGER rmh at temple.edu
Mon Oct 5 21:52:25 CEST 2009


> tmp2 <- matrix(1:8,4,2)
> dimnames(tmp2)
NULL
> tmp2
     [,1] [,2]
[1,]    1    5
[2,]    2    6
[3,]    3    7
[4,]    4    8
> dimnames(tmp2)[[2]] <- c("a","b")
> tmp2
     a b
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
> tmp1 <- matrix(1:4,4,1)
> dimnames(tmp1)
NULL
> tmp1
     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
> dimnames(tmp1)[[2]] <- "a"
Error in dimnames(tmp1)[[2]] <- "a" : 'dimnames' must be a list
## this error caught me by surprise.  Since this is a replaement,I
think it should work parallel
## to the two column case.  My reading of the ?Extract description of drop
   drop: For matrices and arrays.  If ‘TRUE’ the result is coerced to
the lowest possible dimension (see
          the examples).  This only works for extracting elements, not
for the replacement.  See ‘drop’
          for further details.
suggests that this should work for the one column case.

Two alternate methods that do work are
> dimnames(tmp1) <- list(1:4,"a")
> colnames(tmp1) <- "a"

Rich




More information about the R-help mailing list