unlist() function in R with Example

To create a list in R, use the list() method. But in some scenarios, we have to convert a list into a vector, and to do that, we can use the unlist() function. The unlist() function interprets the list to produce a vector that contains all the atomic components which occur in the input list.

unlist() function in R

The unlist() function is used to convert a list to vector in R. The unlist() function takes the list as an argument and returns the Vector. It flattens the list.

Syntax

unlist(list, recursive = TRUE, use.names = TRUE)

Parameters

list: It is an R object, typically a list or vector.

recursive: It is a logical value that should unlist be applied to list components of x?

use.names: It is logical. Should names be preserved?

Return Value

The unlist() method returns a vector.

The output type is determined from the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression, after coercion of pairlists to lists.

Example

The unlist() function converts a list of vectors into a single vector. Let’s define a list of vectors and pass that list to the unlist() function. To create a list in R, use the list() function.

data <- list(c(11, 18, 19, 21, 46))
class(data)
rv <- unlist(data)
rv
class(rv)

Output

[1] "list"
[1] 11 18 19 21 46
[1] "numeric"

As you can see, the unlist() function returns the vector, and in our case, it is a numeric vector.

Unlist the List of Data Frames

We can create a list that contains vector and data frame.

First, let’s define a list with two vectors.

data <- list(c(11, 18, 19, 21),
             c(4, 10, 20, 46))
mix <- data
mix

Here, the data list only contains vectors. Nothing else.

Now, we will append a new list item, which will be a data frame. To create a data frame, use the data.frame() function.

To append an element to the list, use the double box brackets ( [[ ]] ).

mix[[3]] <- data.frame(x1 = c(5, 1, 2),
                       x2 = c(7, 5, 7))

Let’s see the mix list in the output.

data <- list(c(11, 18, 19, 21),
             c(4, 10, 20, 46))
mix <- data
mix
cat("-------------------------", "\n")
cat("Modified list is: ", "\n")
cat("-------------------------", "\n")
mix[[3]] <- data.frame(x1 = c(5, 1, 2),
                       x2 = c(7, 5, 7))
mix

Output

[[1]]
[1] 11 18 19 21

[[2]]
[1] 4 10 20 46

-------------------------
Modified list is:
-------------------------
[[1]]
[1] 11 18 19 21

[[2]]
[1] 4 10 20 46

[[3]]
  x1   x2
1  5   7
2  1   5

Now, use the unlist() function to unpack the list and print the elements one by one.

data <- list(c(11, 18, 19, 21),
             c(4, 10, 20, 46))
mix <- data
mix
cat("-------------------------", "\n")
cat("Modified list is: ", "\n")
cat("-------------------------", "\n")
mix[[3]] <- data.frame(x1 = c(5, 1, 2),
                       x2 = c(7, 5, 7))
mix
cat("-------------------------", "\n")
cat("Unpacked list is: ", "\n")
cat("-------------------------", "\n")
unlist(mix)

Output

[[1]]
[1] 11 18 19 21

[[2]]
[1] 4 10 20 46

-------------------------
Modified list is:
-------------------------
[[1]]
[1] 11 18 19 21

[[2]]
[1] 4 10 20 46

[[3]]
   x1  x2
1  5   7
2  1   5
3  2   7

-------------------------
Unpacked list is:
-------------------------
                              x11 x12 x13 x21 x22 x23
 11  18  19  21  4  10  20 46  5   1   2   7   5   7

As you can see in the output that each column of the data matrix is unlisted itself.

Convert data frame to vector

To convert a data frame to vector in R, use the unlist() function. 

rl <- unlist(BOD)
rl

Output

  Time1  Time2  Time3  Time4  Time5  Time6  demand1  demand2  demand3  demand4
   1.0    2.0    3.0    4.0    5.0    7.0    8.3       10.3     19.0    16.0
  
  demand5  demand6
   15.6     19.8

If you want just values and not column names, then you can pass the use.names = FALSE.

rl <- unlist(BOD, use.names = FALSE)
rl

Output

 [1] 1.0  2.0  3.0  4.0  5.0  7.0  8.3  10.3  19.0  16.0  15.6  19.8

As you can see, we removed the names and print only the column values, and to do that, and we used use.names = FALSE as an argument to unlist() function.

Unlist a list of character vectors in R

To unlist a character vector in R, use the unlist() function.

rs <- list(c("A", "B", "X", "Y"))
ul <- unlist(rs)
ul

Output

[1] "A"  "B"  "X"  "Y"

That is it for the unlist() function in the R tutorial.

 

Leave a Comment