8

Is there a command, or method for adding descriptive text to a variable such that when you call for the str() of the variable you also see an attribute that describes what's in it?

I find, in some cases that commenting my code is simply not enough (especially when working with a lot of variables).

Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255

1 Answers1

6

Brandon,

comment() and attr() can be useful here. This recent post has some really good information on this.

From the help page for comment():

x <- matrix(1:12, 3,4)
comment(x) <- c("This is my very important data from experiment #0234",
                "Jun 5, 1998")
x
comment(x)

and str(x) returns:

> str(x)
 int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
 - attr(*, "comment")= chr [1:2] "This is my very important data from experiment #0234" "Jun 5, 1998"
Community
  • 1
  • 1
Chase
  • 67,710
  • 18
  • 144
  • 161