Accessing list elements in R by name

TIL: parts of the element name are sufficient

R

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

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

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

Code
mylist$frequency
[1] 0.193

But we can also do this:

Code
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!

Code
mylist$freestyle = 0.479

Now we can’t do this anymore:

Code
mylist$fre
NULL