R Arrays Are Not What You Think
When you're learning a programming language, it's often helpful to review the basics. I've been providing quick articles on the R programming language - some of the basic data structures and data types. Take a minute with me and review the array data structure.
Arrays in R are different than Arrays in Python
In every other language, an array is a collection of values. You access those values by index or name, so you'll typically see something like myArray[3] - which is the third item of a variable called "myArray." But that's not the most useful way to think of arrays in R.
Vector = 1 dimension, Matrix = 2 dimensions, Array = 3 or more dimensions
Think of arrays in context with vectors and matrices. So for example, here's a vector...
I.am.a.vector <- c("twas","brillig","and","the","slithey","toves","did","gyre","and","gimble","in","wabe")
> I.am.a.vector
[1] "twas" "brillig" "and" "the" "slithey" "toves" "did" "gyre" "and" "gimble" "in" "wabe"
In R, a vector looks like a python array - or an array in most other languages. But it's a vector and has one dimension - width.
You can convert a vector to a matrix just by adding a number of rows (or columns)...
> I.am.a.matrix <- matrix(I.am.a.vector, nrow = 3)
> I.am.a.matrix
[,1] [,2] [,3] [,4]
[1,] "twas" "the" "did" "gimble"
[2,] "brillig" "slithey" "gyre" "in"
[3,] "and" "toves" "and" "wabe"
Note that the number of columns in that matrix are automatically calculated. If you have twelve elements in a vector and you divide them into three rows, then you MUST have four columns. Matrices in R have two dimensions (width and height)
So...you can convert a vector to an array by adding rows, columns, and levels. Let's take I.a.an.array and divide it into two matrices, each with two rows and three columns...
Recommended by LinkedIn
> I.am.an.array <- array(I.am.a.vector,c(2,3,2) )
> I.am.an.array
, , 1
[,1] [,2] [,3]
[1,] "twas" "and" "slithey"
[2,] "brillig" "the" "toves"
, , 2
[,1] [,2] [,3]
[1,] "did" "and" "in"
[2,] "gyre" "gimble" "wabe"
)
Essentially, we've created two matrices and stacked them on top of each other. Want to get a number out of the array? Just use three indexes...
> I.am.an.array[2,3,2]
[1] "wabe"
Want to see more?
I publish a weekly video on R topics. Here's the video on Arrays...
By the way...
I write Science Fiction. If you're a member of goodreads, you can win a copy (between August 19 and September 17, 2021).
I’m not always a huge fan of science fiction but this is well worth reading.
It takes place in 2062. Auto-driving cars and no accidents in 20 years. Then suddenly an accident and somebody dies and the only one who knows is.... a refrigerator.
Seriously, it sounds ridiculous but it works. I now want to read more science fiction books.
DethWench Professional…•8K followers
4yHi Mark Niemann-Ross I'm going to bookmark this, because I love how you show how the array, matrix, and and vector look different in #rstats - especially how the array and matrix look. I live in dataframe land, but sometimes, I need to use arrays and matrices and I always forget how to handle them. This is so different in #sasprogramming - Daniel Wanjiru you should really read these posts from Mark, because you can see how SAS arrays and R arrays are very different, and how R is so much easier to use for data handling than SAS (although it's hard to handle #bigdata - unfortunately!).