How to set attributes for a variable in R?

How to assign an attribute to a variable? E.g.

> x <- rpart(f.la, mydata) 

assigns attributes:

 $names [1] "frame" "where" [3] "call" "terms" [5] "cptable" "method" [7] "parms" "control" [9] "functions" "numresp" [11] "splits" "variable.importance" [13] "y" "ordered" $xlevels named list() $ylevels [1] "cancelled" "cart-abandon" "purchased" "returned" $class [1] "rpart" 

Similarly, I want to create attributes for a variable and assign a value to this attribute.

thanks

+8
r attributes
source share
2 answers

Alternatively, using attributes ( see @CathG answer ), you can use attr . The first will work with NULL objects, and the second will not. When you work with R attributes, you should remember that they are not simple as they look, and can have some interesting side effects. Quick example:

 > x <- 1:10 > class(x) [1] "integer" > x [1] 1 2 3 4 5 6 7 8 9 10 

So far so good. Now you can set the attribute dim

 > attr(x, 'dim') <- c(2, 5) > class(x) [1] "matrix" > x [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 

class attribute is a fundamental part of S3 classes :

 > foo <- list() > foo list() 

Let's see what happens if we set the class attribute to 'data.frame'

 > attr(foo, 'class') <- 'data.frame' > foo data frame with 0 columns and 0 rows 

or we can define custom behavior (BTW this behavior is the reason that it is better to avoid dots when defining functions):

 > print.foo <- function(x) cat("I'm foo\n") > attr(foo, 'class') <- 'foo' > foo I'm foo 

Other attributes, such as comment and names , also have special meanings and limitations. Remove the message here, you need to be a little careful when working with attributes in R. One simple idea to deal with is to use prefixes as artificial namespaces:

 > x <- 1:10 > attr(x, 'zero323.dim') <- c(2, 5) > class(x) [1] "integer" > x [1] 1 2 3 4 5 6 7 8 9 10 attr(, 'zero323.dim') [1] 2 5 

In my opinion, this is especially useful when using third-party libraries. The use of attributes is usually poorly documented, especially when used for some internal tasks, and it is quite easy to introduce some difficulties for diagnosing errors if you use conflicting names.

+13
source share

If you have a variable:

 x<-"some variable" 

You can do

 attributes(x)$myattrib<-"someattributes" > x [1] "some variable" attr(,"myattrib") [1] "someattributes" 

or

 > attributes(x) $myattrib [1] "someattributes" 
+3
source share

All Articles