In R, what is the difference between a1 and b1 :
a1 = structure(list(c("A","B"), c("X","Y")))
b1 = list(c("A","B"), c("X","Y"))
I'm asking this question because I'm trying to understand how the dataset UCBAdmissions has been built. We have
structure(c(512, 313, 89, 19, 353, 207, 17, 8, 120, 205, 202,
391, 138, 279, 131, 244, 53, 138, 94, 299, 22, 351, 24, 317), .Dim = c(2L,
2L, 6L), .Dimnames = structure(list(Admit = c("Admitted", "Rejected"
), Gender = c("Male", "Female"), Dept = c("A", "B", "C", "D",
"E", "F")), .Names = c("Admit", "Gender", "Dept")), class = "table")
Without using structure for .Dimnames, it doesn't work and I don't know why.
structure is a function to associate attributes with a given R object. For example:
a <- 1:4
b <- structure(a, x="an attribute")
b
#[1] 1 2 3 4
#attr(,"x")
#[1] "an attribute"
This does the same thing as assigning attributes to an object with attr(*) <-:
b <- a
attr(b,"x") <- "an attribute"
b
#[1] 1 2 3 4
#attr(,"x")
#[1] "an attribute"
Certain common features you probably use all the time are implemented as attributes. These include names, dim (array dimensions) and dimnames (row and column names, and multidimensional extensions thereof).
structure(1:4, dim=c(2,2), dimnames=list(c("row1","row2"), c("col1","col2")))
# col1 col2
#row1 1 3
#row2 2 4
Another one is an object's S3 class.
obj <- structure(1:4, class="myclass")
print.myclass <- function(x) {
cat("An object of class 'myclass'\n")
print.default(c(x))
invisible(x)
}
obj
#An object of class 'myclass'
#[1] 1 2 3 4
In your case, UCBAdmissions is a table object, which is basically a multidimensional array with optional names for each of the dimensions. The output you have there is from dput, which is more verbose than strictly necessary. This is for conservative reasons, to ensure that the output can always be parsed correctly. You can simplify it a bit:
# from dput(UCBAdmissions)
x <- structure(c(512, 313, 89, 19, 353, 207, 17, 8, 120, 205, 202, 391,
138, 279, 131, 244, 53, 138, 94, 299, 22, 351, 24, 317),
.Dim = c(2L, 2L, 6L),
.Dimnames = structure(list(Admit = c("Admitted", "Rejected"),
Gender = c("Male", "Female"),
Dept = c("A", "B", "C", "D", "E", "F")),
.Names = c("Admit", "Gender", "Dept")),
class = "table")
# simplified
y <- structure(c(512, 313, 89, 19, 353, 207, 17, 8, 120, 205, 202, 391,
138, 279, 131, 244, 53, 138, 94, 299, 22, 351, 24, 317),
dim = c(2, 2, 6),
dimnames = list(Admit = c("Admitted", "Rejected"),
Gender = c("Male", "Female"),
Dept = c("A", "B", "C", "D", "E", "F")),
class = "table")
identical(x, y)
# TRUE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With