I have simple R code as follows:
change <- function(x, v) {
attr(x, 'abc') <- v
attributes(x)
}
change(iris$Sepal.Length, 't1')
attributes(iris$Sepal.Length)
the result shows:
> change(iris$Sepal.Length, 't1')
$abc
[1] "t1"
> attributes(iris$Sepal.Length)
NULL
as you can see, attributes(x) printed inside the function shows x (which is iris$Sepal.Length) has the attribute abc as t1. but after function is called, attributes(iris$Sepal.Length) shows 'abc' is not defined anymore.
Anyone knows why? How can I change object's attribute inside a function using passed arguments?