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?
r
tonytonov
source share