Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Descriptive Text to a Variable

Tags:

r

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).

like image 254
Brandon Bertelsen Avatar asked Mar 19 '11 00:03

Brandon Bertelsen


1 Answers

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"
like image 153
Chase Avatar answered Nov 03 '22 09:11

Chase