Skip to content

Assigning to a vector while keeping the attributes

3 messages · Alon Wasserman, David Winsemius, Gabor Grothendieck

#
Not sure what "a" really is. It's not a vector or a list according to  
the R interpreter.

 > a <- structure(1:3,x=3)
 > mode(a)
[1] "numeric"
 > is.vector(a)
[1] FALSE
 > is.list(a)
[1] FALSE

Experimentation shows that simple indexing provides the functionality  
you request while append does not.

 > a[4] <- 10
 > a
[1]  1  2  3 10
attr(,"x")
[1] 3


 > a <-append(a, 20)
 > a
[1]  1  2  3 10 20

# appears to have now coerced it to a vector
 > a
[1]  1  2  3 10 20
 > is.vector(a)
[1] TRUE
#
Try this:

a <- structure(1:3, x = 3)
b <- "attributes<-"(11:15, attributes(a))
dput(b)
On Mon, Feb 9, 2009 at 7:09 AM, Alon Wasserman <alon.was at gmail.com> wrote: