I have created an object in R that contains several attributes. How can I easily access them?
I can do:
attr(x, attributeName)
or:
attributes()
returns a named list. I'd call it once and store them, then access via names. There is no point repeatedly calling either attr()
or attributes()
if you don't have to.
x <- 1:10
attr(x, "foo") <- "a"
attr(x, "bar") <- "b"
(features <- attributes(x))
which gives:
R> (features <- attributes(x))
$foo
[1] "a"
$bar
[1] "b"
then access in the usual way
R> features["foo"]
$foo
[1] "a"
R> features$foo
[1] "a"