I am a little bit stuck with one exercise in a beginner R course that I need for the following exercises (we should replace values of the previously created object).
Create object A, which returns the following when the structure is queried:
> str(A)
num [1 : 2, 1 : 5, 1 : 3] TRUE FALSE TRUE FALSE TRUE FALSE ...
- attr(, "dimnames")=List of 3
..$ : chr [1:2] "a" "b"
..$ : chr [1:5] "C1" "C2" "C3" "C4" ...
..$ : chr [1:3] "X" "Y" "Z"
Because I am a little bit clueless with the content, that is my beginning:
a <- rep(c(0,1),15)
A <- array(a, dim= c(2,5,3))
rownames(A) <- letters[1:2]
colnames(A) <- paste("C",1:5,sep="")
Unfortunately I struggle with the object itself, I don't see how the array should be filled to be numeric and have a TRUE/FALSE content. Also the naming of the third dimension is something where I didn't find sufficient information.
Can anyone help me here?
This exercise appears to be trying to teach you about array dimensions.
array has 3 arguments:
args(array)
#function (data = NA, dim = length(data), dimnames = NULL)
data = is the data to be put in the array. Replacement is allowed.dim = an integer vector giving the "maximal indices in each dimension"dimnames = is a list of character vectors each as long as the corresponding dimension. (As an aside, the character vectors themselves can also be named)Thus, the following would get pretty close to your desired output:
A = array(data = c(TRUE,FALSE),
dim = c(2,5,3),
dimnames = list(c("a","b"), c("C1","C2","C3","C4","C5"),c("X","Y","Z")))
str(A)
# logi [1:2, 1:5, 1:3] TRUE FALSE TRUE FALSE TRUE FALSE ...
# - attr(*, "dimnames")=List of 3
# ..$ : chr [1:2] "a" "b"
# ..$ : chr [1:5] "C1" "C2" "C3" "C4" ...
# ..$ : chr [1:3] "X" "Y" "Z"
However, I do not see a way for str to print TRUE FALSE TRUE FALSE TRUE FALSE ... while also being class num. Perhaps the lesson is incorrect.
You could also try your approach, but use dimnames(A)[3] to assign the third dimension's names:
dimnames(A)[3] <- list(c("X","Y","Z"))
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