How to Flatten a List into a Vector in R

04.07.2021

A list is a multidimensional data structure in R. It can hold keys and lists of lists. Often, you would like to extract the values from a list. You can do this by "flattening" it into a vector. In this article, we will learn how to fallten a list into a vector in R.

For example, let's say we have a list of numbers. We can't use normal functions like mean on this as the list is multidimensional, hence why we use the double brackets to access. We can use the unlist function to turn the list into a vector.

scores = list(10, 20, 30, 40)

scores.vec = unlist(scores)

mean(scores.vec)