Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

structure and attribute in R

I am going over the book Advanced R by Hadley. The book has a question which makes me really confused:

> structure(1:6, comment = "my attribute")
[1] 1 2 3 4 5 6

When you print that object you don't see the comment attribute. Why? Is the attribute missing, or is there something else special about it?

Could anyone help me understand what's going on here?

like image 445
hi15 Avatar asked Jan 23 '15 00:01

hi15


People also ask

What is a structure in R?

R's basic data structures include the vector, list, matrix, data frame, and factors. Some of these structures require that all members be of the same data type (e.g. vectors, matrices) while others permit multiple data types (e.g. lists, data frames). Objects may have attributes, such as name, dimension, and class.

What is an attribute in R programming?

Objects in R can have many properties associated with them, called attributes. These properties explain what an object represents and how it should be interpreted by R. Quite often, the only difference between two similar objects is that they have different attributes.


1 Answers

An attribute named "comment" is treated specially by R's default print method. From ?comment:

Description:

     These functions set and query a _comment_ attribute for any R
     objects.  This is typically useful for ‘data.frame’s or model
     fits.

     Contrary to other ‘attributes’, the ‘comment’ is not printed (by
     ‘print’ or ‘print.default’).
like image 67
Josh O'Brien Avatar answered Nov 14 '22 23:11

Josh O'Brien