Structure in R is the way to provide some attribute to a list or a vector or to change their behavior.
a <- structure(list(names=c("John", "Mick"), age=c(25, 29)), type= "people")
In this way we are going to create structure that is going to contain list that contains two vectors named "names" and "age", and one attribute called "type". We can get data from those vectors in the same way like with any lists, but in order to get attribute value we have to use function attr like attr(a, "type"). Function attr requares two parameters; first is the structure name, and the second is name of attribute we want to get.
If you want to change behavior of a list, you can do that in next way:
a <- structure(list(names=c("John", "Mick"), age=c(25, 29)), class="data.frame", .Names=c("names", "age"), row.names= c("1", "2"))
Class attribute is going to change behavior of list a to that of data.frame. With argument .Names we are going to set names for our columns, and with argument row.names, we are going to set names for our rows.
Video tutorial link below:
No comments:
Post a Comment