Skip to content
Prev 171601 / 398503 Next

learning R

On Feb 24, 2009, at 11:36 PM, Fuchs Ira wrote:

            
I cannot tell you why, perhaps you are not actually working with the  
names of a, but I can show you that:

 > names(a)[2] <- "too"
 > a[2]
too
   2
 > a
one too
   1   2

And this is seen as well in the help page examples. The help page also  
says the following, which I cannot understand:

It is possible to update just part of the names attribute via the  
general rules: see the examples. This works because the expression  
there is evaluated as z <- "names<-"(z, "[<-"(names(z), 3, "c2")).
That is ok. Also effective would be:

z <- matrix( c(x,y), ncol=length(x) )
Not sure why you are trying to do that to x since it is a vector but  
it can be done if you make it a matrix first. Take a look at these and  
see if you can figure out what is happening:

 > x <- matrix(x,2,3)  # second and third arguments of matrix function  
are nrow and ncol.
 > x
      [,1] [,2] [,3]
[1,]    1    3    2
[2,]    2    1    3

And then try:
 > x = c(1,2,3)
 > x <- matrix(x,2,3,byrow=TRUE)
 > x
      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    2    3

And now that x is a matrix, this will work:

 > x[2,] <- y
 > x
      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    3    4    5

-- David Winsemius