R Arrays Are Not What You Think

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...

> 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.
Monika Wahi

DethWench Professional…8K followers

4y

Hi 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!).

To view or add a comment, sign in

More articles by Mark Niemann-Ross

  • Documenting My Code ... For Me

    There are two signs of old age: old age, and ..

  • R Meets Hardware

    R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data…

    2 Comments
  • Party Buzz Kill: modifying data

    So Steve (SQL), Marsha (C), Bob (Python), and I (R) are at this party. We have TOTALLY cleared the room, especially now…

    2 Comments
  • Rain - Evapotranspiration = mm Water

    "Eeee-VAP-oooo-TRANS-PURR-ation," I savor the word as I release it into our conversation. I'm still at the party with…

  • Party Buzz Kill: Data Storage

    I'm at this party where Bob and Marsha and I are discussing the best languages for programming a Raspberry Pi. Bob…

    5 Comments
  • R Waters My Garden

    I'm at a party, and the topic of programming languages comes up. A quarter of the room politely leaves, another half…

    10 Comments
  • Caning and Naming

    We've been back from Port Townsend for a week. Progress on the boat isn't as dramatic as it is when we're spending the…

    1 Comment
  • Irrigate with R and Raspberry Pi

    I’m working on my irrigation system. This requires a controller to turn it on and off.

    3 Comments
  • 5 Reasons to Learn Natural Language Processing with R

    Why learn R? Why learn Natural Language Processing? Here's five reasons..

    1 Comment
  • Performing Natural Language Processing with R

    I recently released a course on Educative covering topics in Natural Language Processing. Different Learners -…

    1 Comment

Others also viewed

Explore content categories