Convert List to Vector

To convert List to Vector in R, call unlist() function and pass the list as argument to the function.

In this tutorial, we will learn how to convert a list to vector in R, using unlist() function, with the help of example program.

Example

In the following program, we create a list with three elements, and convert it into vector using unlist().

Example.R

x <- list(18, 25, 33)
y <- unlist(x)
print(y)

Output

[1] 18 25 33

List with Different Datatypes

If list contains elements of different datatype, then lesser datatypes would be promoted to the highest datatype.

In the following program, we take a list with logical, double and character elements. When we convert this list to vector, logical and double values would be promoted to character datatype.

Example.R

x <- list(TRUE, 25, "Apple")
print(x)

y <- unlist(x)
print(y)

Output

[[1]]
[1] TRUE

[[2]]
[1] 25

[[3]]
[1] "Apple"

[1] "TRUE"  "25"    "Apple"
ADVERTISEMENT

Conclusion

In this R Tutorial, we learned how to convert a List into Vector in R programming using unlist() function, with the help of examples.