Back to basics: Named Vectors
Photo: Christina Morillo at Pexels

Back to basics: Named Vectors

In the previous newsletter I reviewed R vectors - which are like standard programming arrays - sort of - but not really. They are more like one-dimensional arrays, but that's for a later newsletter...

Before we move on to other R data structures, I wanted to put another R feature into context with other programming languages. In this case, let's compare R's named vectors with python dictionaries - which are also described as associative arrays. All of these have key-value structures. You ask for the key - and get the value.

# here's a python dictionary...
ImAKeyValue = {'first': "twas", 'second': "brillig"}

# here's a named vector in R
ImAKeyValue <- c('first' = "twas",'second' = "brillig")

        

Both of these allow me to access a value by name. Here's how that looks in R...

> ImAKeyValue["first"]
 first 
"twas"        

If you want multiple values, use c()...like this...

ImAKeyValue[c("first","second")]
    first    second 
   "twas" "brillig"        

You'll often see named vectors in R defined in two steps. Here's an example...


ImAKeyValue <- c("twas","brillig")
names(ImAKeyValue) <- c("first","second")
        

Both forms produce the same result.

By the way...

I've produced a "quick" reference to matrix math functions in R. You can see it here.


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

Insights from the community

Others also viewed

Explore topics