Ask any data scientist what his programming language of choice is and there’s a strong chance that it’ll be R. Part of this popularity stems from the wide variety of native data types. These components include containers like the data frame and individual vector entities. You can use a data frame or numeric vector to work with almost any given research methodology or collection of data.
But there’s another reason for R’s popularity in fields related to data science. And that’s the fact that all of these powerful data types are extraordinarily flexible. R’s data types all have an element of coercion to them. This means that they can be assigned traits of one type and then shift over to a whole new rule set as needed by turning them into another object type. For example, consider how R works with the list type. You can easily use R unlist to convert lists into vectors. And you’re about to see exactly how it’s done.
Working With Lists in R
Before manipulating and converting lists, we need to take a moment to consider the nature of the structure itself. Lists in R function as a general-purpose container. This means that the containers essentially share their nature with other R types since they contain these items. For example, lists aren’t really a numeric type. But you can often perform numeric-dependent operations on lists because of the container’s contents.
In fact, one of the interesting points about lists is that they can even hold multiple types. For example, you could create a list containing both a number and a string. You might imagine that this versatility would make it hard to convert lists into different types. But the secret to working with lists is that we often don’t manipulate the list. We instead directly access individual elements in the lists, such as a single vector, to convert between types.
The Basics of Unlist and List Manipulation
We can extract elements from lists, and convert them between types, through a number of different methodologies. One relatively simple method is to loop through a list’s contents. And R has some advanced looping systems like lapply that could make the process relatively easy. But data manipulation is one of R’s greatest strengths. And we have some excellent options for even more efficient methods of extracting data from a list. One of the absolute best is the previously mentioned R unlist function.
The unlist function takes an R list as an argument and returns a vector. And it really can be just that simple. Take a look at the following code to see unlist in action.
ourList <- list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
ourVector <- unlist(ourList)
In this example, we start out by creating ourList. It consists of the numbers 1 to 10. This is one of the simpler lists that you’ll encounter since we just want to take a look at the most basic example of the unlist function in action. And we see just that when we pass ourList to unlist and assign the resulting vector to ourVector. We print the result out on the next line and can verify that everything is exactly as one would hope. The conversion from list to vector really did just take a single line of code and a single argument. Of course, as previously noted, this example is simple by design. Unlist, even at its most complex, is still generally going to be fairly straightforward to use. But there are a few usage scenarios that deserve extra attention.
More Advanced Conversion Options
One of the more common unlist concerns people have is related to type conflict. For example, what if you had some list element types as string and others as a numeric vector? Or even logical constants within your lists before conversion? Would the character sets found in a string need extra work? Would there be any conflicts between the types when passing them unaltered into a function? This seems like it’d be a difficult proposition. But take a look at the following example.
ourList <- list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, “Bob”, “Bill”, “Sally”)
ourVector <- unlist(ourList)
We have both proper names in string format and numbers within ourList. However, in the next line we simply pass ourList to unlist without using any sort of additional conversion to take account of that fact. The only caveat is that we look at the contents of ourVector using cat rather than print in order to retain proper formatting.
We can even use unlist with the contents of a data frame. Try running the following code.
df <- data.frame(
planets = c(“Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”),
moons = c(0, 0, 1, 2, 79, 82, 27, 14, 5),
distanceFromSun = c(57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1, 5906.4)
moonsColumn <- df$moons
ourMoons <- unlist(moonsColumn)
We begin by filling a new dataframe with various value entries that correspond to planets. Next, we begin to see how this is going to go in a more advanced direction. A specific column from the dataframe is used as an input list for unlist. This highlights that we can easily work with the data structure of R’s various types to fit all of our moving pieces into place for conversion. What matters is the data rather than where we might have procured it from. Remember, a container isn’t its contents. What really matters is the data structure held within the larger list structure or dataframe.
By specifying a column we’re not just trying to filter out the other information. We’re also narrowing the data down so that we can have more control over both the output and the output type. R converts a wide variety of data types. But it’s still important to ensure we always know what we’re working with. And care when selecting data from a container is a big part of that practice. But with that in mind, the list to vector conversion can be seen in action on the next line as we pass the new moonsColumn to unlist. We assign the result to ourMoons and use print on the result. And in doing so we see that the list to vector conversion has been executed successfully.