Accessing list elements in R by name

TIL: parts of the element name are sufficient

R
Authors
Affiliations

Let’s create a list mylist with two elements, mylist$frequency and mylist$amplitude.

mylist <- list()
mylist$frequency <- 0.193
mylist$amplitude <- 0.603

If we want to see the value of mylist$frequency, we can do this:

mylist$frequency
[1] 0.193

But we can also do this:

mylist$fre
[1] 0.193

We now add another element to the list called mylist$freestyle.

Note, that freestyle also starts with the letters fre!

mylist$freestyle = 0.479

Now we can’t do this anymore:

mylist$fre
NULL