40

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(x)$attributeName

but none of them is really convenient.

Is there a quicker way (like the dot in C++ or Java)?

4 Answers 4

27

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"
14

Don't use attributes for your object, use a list:

myobj <- structure(list(a = 1, b = 2), class = "myclass")
print.myclass <- function(x, ...) cat("A: ", x$a, " B: ", x$b, "\n", sep = "")
myobj

Of course, this might not work if you're extending an existing object (e.g. vector), but in my experience it's a generally better way to build objects.

3
  • 19
    The question is how to access attributes. Your answer is "don't do that" which is not an answer to the question that was asked. There are other people who are searching for answers to the same question. Dec 12, 2016 at 1:16
  • I agree, what if a function returns an object with attributes? This is the answer that comes up.
    – CapnShanty
    Jul 17, 2018 at 17:42
  • ^ I think his point is that a 'correct' answer to this question doesn't exist, so he is giving a reasonable workaround. It is a good answer to the question, if you're already given attributes to work with then just use attr(x, 'y')
    – profPlum
    Dec 11, 2020 at 18:11
11

probably there is no built-in function that is counter part of . in C++, but you can define it like this:

> `%.%` <- function(o, a) attr(o, as.character(substitute(a)))
> x <- 1
> attr(x, "orz") <- 2
> x%.%orz
[1] 2
2
  • Thanks. I am now looking at S4 classes that allo object@attr Jun 23, 2011 at 7:58
  • 4
    Choosing a class system based on how attributes/fields/slots are accessed is probably not the best way to make a decision.
    – hadley
    Jun 23, 2011 at 16:22
2

Example of using the match.length attribute that is returned from regexpr:

Three strings in a vector, first and third include an embedded string:

data=c("<a href=\"ch4.html\">Chapter 1</a>",
       "no quoted string is embedded in this string",
       "<a   href=\"appendix.html\">Appendix</a>")

Use regexpr to locate the embedded strings:

> locations <- regexpr("\"(.*?)\"", data)

Matches are in the first string (at 9 with length 10) and third string (at 11 with length 15):

> locations
[1]  9 -1 11
attr(,"match.length")
[1] 10 -1 15
attr(,"useBytes")
[1] TRUE

Vector from the attribute:

> attr(locations,"match.length")
[1] 10 -1 15

Use substr and the attribute vector to extract the strings:

> quoted_strings=substr( data, 
                         locations, 
                         locations+attr(locations,"match.length")-1 )    
> quoted_strings
[1] "\"ch4.html\""      ""                  "\"appendix.html\""

Maybe you'd like to remove the embedded quote characters from your strings:

> gsub("\"", "", quoted_strings)
[1] "ch4.html"      ""              "appendix.html"

An alternative is to use regmatches:

> regmatches(data,locations)
[1] "\"ch4.html\""      "\"appendix.html\""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.