How do I access the attributes of an atomic vector?

I have set attributes for a variable using the attr function as shown below:

x <- 1 :20
attr(x,'name') <- c("RED","BLUE")
attributes(x)

$name
[1] "RED"  "BLUE"

      

Now that I have set the attributes; if I turn to him with '$

', he says "Error : $ operator is invalid for atomic vectors"

. I have also tried x['name']

which showsNA

How do I access these attributes of an atomic vector?

+3


source to share


1 answer


If I understand your question,

 attr(x, 'name')
#[1] "RED"  "BLUE"

      



or

 attributes(x)$name
#[1] "RED"  "BLUE"

      

+5


source







All Articles