Why are names (x) better than attr (x, "names")?

I am reading an Advanced R topic on data structures and attributes. It says:

You should always get and set these attributes using your function accessories: use the names (x), class (x) and dim (x), not attr (x, "names"), attr (x, "class") and attr (x, "dim").

What is the excuse for this? Is there an example of unexpected behavior? Or is it just a recommendation? At a trivial level, I see no difference:

v <- 1:2 names(v) <- 3:4 all(attr(v, "names") == names(v)) #[1] TRUE attr(v, "names") <- 5:6 all(attr(v, "names") == names(v)) #[1] TRUE 

I tried a more complex approach by looking at the source , namely do_names and do_attributes . I see that the difference is significant, so names(x) not just an alias for attr(x, "names") . I would say that the first is apparently faster, but this is a wild assumption.

As an additional question, is there a difference between names() , class() and dim() from this point of view?

+7
r
source share
1 answer

You should not access attributes directly because the author of the code must provide an API to use to access them. This gives them the ability to change the base code without changing the API.

The xts package is a good example:

 > library(xts) > x <- xts(1:3, as.POSIXct("2014-01-01")+0:2) > index(x) [1] "2014-01-01 00:00:00 CST" "2014-01-01 00:00:01 CST" "2014-01-01 00:00:02 CST" > attr(x, "index") [1] 1388556000 1388556001 1388556002 attr(,"tzone") [1] "" attr(,"tclass") [1] "POSIXct" "POSIXt" 

At some point in the past, the internal index was stored as POSIXct , but we changed the basic structure for performance reasons. However, you can see that the public API has not changed.

+7
source share

All Articles