Exercise 1
Create an array (3 dimensional) of 24 elements using the dim()
function.
Exercise 2
Create an array (3 dimensional) of 24 elements using the array()
function.
Exercise 3
Assign some dimnames of your choice to the array using the dimnames()
function.
Exercise 4
Assign some dimnames of your choice to the array using the arguments of the array()
function.
Exercise 5
Instead of column-major array, make a row-major array (transpose).
Exercise 6
For this exercise, and all that follow, download this file, and read it into R using the read.csv()
function, e.g.:
temp
Copy the column named N
into a new variable arr
.
Exercise 7
Set dimensions of this variable and convert it into a 3 * 2 * 4 array. Add dimnames.
Exercise 8
Print the whole array on the screen.
Exercise 9
Print only elements of height 2, assuming the first dimension represents rows, the second columns and the third heigth.
Exercise 10
Print elements of height 1 and columns 3 and 1.
Exercise 11
Print element of height 2, column 4 and row 2.
Exercise 12
Repeat the exercises 9-11, but instead of using numbers to reference row, column and height, use dimnames
.
Image: Cubo completato” by Masakazu “Matto” Matsumoto from Nagoya, Japan – http://flickr.com/photos/vitroids/1527092739/. Licensed under CC BY 2.0 via Wikimedia Commons.
Would be nice to have solutions to this exercises as well.
Clarification needed for Exercise #7 onwards.
My multi-dimentional array has the dimensions 3*2*4, which I assume corresponds to row*column*height as per E#9. Therefore E#10 and E#11 don’t work, since your number of columns is 2.
Please let me know if I’m misinterpreting this. Thanks!
I like the structure, exercises, and the solutions for validation. I wish if you have a link for array exercise solution
hello
could you please provide solutions for this exercise. i am stuck with questions 9-12
Shared my own solutions in rpubs: http://rpubs.com/onurhan/r-exercises-4-2 . please share your comments!
Thanks a lot! very usefull!
Thank you! I checked your answer on exercise no. 5 and I hadn’t found aperm() myself so that really helped 🙂 I think the exercise wants us to transpose from column-major to row-major input for each level of [, , x]. So if the numbers are 1:24, in each “box” of columns and rows, the numbers should be going “1, 2, 3…” row-wise. Like this:
x y z
a 1, 2, 3
b 4, 5, 6
I’m not well versed enough to understand entirely what happens if we call aperm() with the argument perm = NULL, but it looks like the output you got is slightly different from what the exercise asked us to do.
I tried this instead, and it seems to work for exercise 5:
dnam <- list(c('a', 'b', 'c', 'd'), c('A', 'B'), c('D1', 'D2', 'D3'))
rarr2 <- array(1:24, dim = c(4, 2, 3),
dimnames = dnam)
rarr3 <- aperm(rarr2, perm = c(2, 1, 3))
I hope this helps someone!
Where are the solutions?