Add comments to objects in R

All R objects have attributes – one of these can be a comment. You can set or view the comment attribute of an object using the comment() command. For example:

> x <- c(2, 3, 5, 4, 3, 6)
> comment(x)
NULL

> comment(x) <- "A simple numeric variable"
> comment(x)
[1] "A simple numeric variable"

You have more than one comment, use the square brackets to “define” additional comments.

> comment(x)[2] <- "A second note"
> comment(x)
[1] "A simple numeric variable" "A second note"

Use the comment to remind you what the object is – it is easy to forget later.

Comments are closed.