# Iterating with names

## 2018/12/19

 title: ‘Iterating with names’ author: ’’ date: ‘2018-12-19’ slug: iterating-with-names categories: [ ‘R’ ] tags: [‘tidyverse’, ‘purrr’] type: post

# The Problem

I’ve come across this problem a few times lately, when I’ve wanted to iterate through some sort of named list or vector, and use both the name and value in each iteration.

To illustrate, here’s a vector, which I have creatively named myvec. It is a numeric vector containing the numbers 1 to 26, and each element has a name, which in this case is represented by a letter of the alphabet.

library(purrr)
myvec <- 1:26
names(myvec) <- LETTERS
myvec
##  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

What if I want to iterate through this vector and produce a string, containing both the value and its name?

There are lots of ways to do this, but I want something that feels clean and simple, preferably using base R or tidyverse functions.

If I attempt to solve this problem using lapply(), I quickly realise this won’t work as lapply() doesn’t keep the names of the object being iterated over.

lapply(myvec, function(value){
paste(names(value), " = ", value)
})
## $A ##  " = 1" ## ##$B
##  "  =  2"
##
## $C ##  " = 3" ## ##$D
##  "  =  4"
##
## $E ##  " = 5" ## ##$F
##  "  =  6"
##
## $G ##  " = 7" ## ##$H
##  "  =  8"
##
## $I ##  " = 9" ## ##$J
##  "  =  10"
##
## $K ##  " = 11" ## ##$L
##  "  =  12"
##
## $M ##  " = 13" ## ##$N
##  "  =  14"
##
## $O ##  " = 15" ## ##$P
##  "  =  16"
##
## $Q ##  " = 17" ## ##$R
##  "  =  18"
##
## $S ##  " = 19" ## ##$T
##  "  =  20"
##
## $U ##  " = 21" ## ##$V
##  "  =  22"
##
## $W ##  " = 23" ## ##$X
##  "  =  24"
##
## $Y ##  " = 25" ## ##$Z
##  "  =  26"

This question had been asked previous on Stack Overflow and the next thing I attempted was to use sapply(), which didn’t work either.

sapply(myvec, function(value){
paste(names(value), " = ", value)
}, simplify = FALSE, USE.NAMES = TRUE)
## $A ##  " = 1" ## ##$B
##  "  =  2"
##
## $C ##  " = 3" ## ##$D
##  "  =  4"
##
## $E ##  " = 5" ## ##$F
##  "  =  6"
##
## $G ##  " = 7" ## ##$H
##  "  =  8"
##
## $I ##  " = 9" ## ##$J
##  "  =  10"
##
## $K ##  " = 11" ## ##$L
##  "  =  12"
##
## $M ##  " = 13" ## ##$N
##  "  =  14"
##
## $O ##  " = 15" ## ##$P
##  "  =  16"
##
## $Q ##  " = 17" ## ##$R
##  "  =  18"
##
## $S ##  " = 19" ## ##$T
##  "  =  20"
##
## $U ##  " = 21" ## ##$V
##  "  =  22"
##
## $W ##  " = 23" ## ##$X
##  "  =  24"
##
## $Y ##  " = 25" ## ##$Z
##  "  =  26"

The solution I went with in the end uses purrr and gives me further incentive to get round to learning it properly future!

My fantastic colleague, John Drummond, pointed out that this can be achieved via the use of map() like so:

map(names(myvec),~paste(.x,"=",myvec[[.x]]))
## []
##  "A = 1"
##
## []
##  "B = 2"
##
## []
##  "C = 3"
##
## []
##  "D = 4"
##
## []
##  "E = 5"
##
## []
##  "F = 6"
##
## []
##  "G = 7"
##
## []
##  "H = 8"
##
## []
##  "I = 9"
##
## []
##  "J = 10"
##
## []
##  "K = 11"
##
## []
##  "L = 12"
##
## []
##  "M = 13"
##
## []
##  "N = 14"
##
## []
##  "O = 15"
##
## []
##  "P = 16"
##
## []
##  "Q = 17"
##
## []
##  "R = 18"
##
## []
##  "S = 19"
##
## []
##  "T = 20"
##
## []
##  "U = 21"
##
## []
##  "V = 22"
##
## []
##  "W = 23"
##
## []
##  "X = 24"
##
## []
##  "Y = 25"
##
## []
##  "Z = 26"

However, I prefer the syntax of map2() which provides me with a slightly more readable solution:

map2(myvec, names(myvec), ~paste(.y, "=", .x))
## $A ##  "A = 1" ## ##$B
##  "B = 2"
##
## $C ##  "C = 3" ## ##$D
##  "D = 4"
##
## $E ##  "E = 5" ## ##$F
##  "F = 6"
##
## $G ##  "G = 7" ## ##$H
##  "H = 8"
##
## $I ##  "I = 9" ## ##$J
##  "J = 10"
##
## $K ##  "K = 11" ## ##$L
##  "L = 12"
##
## $M ##  "M = 13" ## ##$N
##  "N = 14"
##
## $O ##  "O = 15" ## ##$P
##  "P = 16"
##
## $Q ##  "Q = 17" ## ##$R
##  "R = 18"
##
## $S ##  "S = 19" ## ##$T
##  "T = 20"
##
## $U ##  "U = 21" ## ##$V
##  "V = 22"
##
## $W ##  "W = 23" ## ##$X
##  "X = 24"
##
## $Y ##  "Y = 25" ## ##$Z
##  "Z = 26"

Do you know any other ways of achieving my goals: iterating through a named list or vector, using both names and values, with an emphasis on readability? Let me know on Twitter.

[Update]

As ever, the R Twitter community is amazing, and in a matter of minutes this even better purrr solution was highlighted by the fantastic @rensa_co (and just minutes later @romain_francois too!)

imap(myvec, ~paste(.y, "=", .x))
## $A ##  "A = 1" ## ##$B
##  "B = 2"
##
## $C ##  "C = 3" ## ##$D
##  "D = 4"
##
## $E ##  "E = 5" ## ##$F
##  "F = 6"
##
## $G ##  "G = 7" ## ##$H
##  "H = 8"
##
## $I ##  "I = 9" ## ##$J
##  "J = 10"
##
## $K ##  "K = 11" ## ##$L
##  "L = 12"
##
## $M ##  "M = 13" ## ##$N
##  "N = 14"
##
## $O ##  "O = 15" ## ##$P
##  "P = 16"
##
## $Q ##  "Q = 17" ## ##$R
##  "R = 18"
##
## $S ##  "S = 19" ## ##$T
##  "T = 20"
##
## $U ##  "U = 21" ## ##$V
##  "V = 22"
##
## $W ##  "W = 23" ## ##$X
##  "X = 24"
##
## $Y ##  "Y = 25" ## ##$Z
##  "Z = 26"

A little bit of context here; bear in mind that I haven’t provided argument names above, simply for the sake of brevity and readability:

imap() is an indexed map and its first argument .x (in this case myvec) is the list or vector.

The second argument (.f) can be specified in a few different ways (as a function, formula, or vector), but here I’ve used the formula syntax (as marked by the ~).

Within the body of the formula, .x refers to the element itself and .y refers to the name of the element.