How to Convert Vector to List in R

Convert Vector to List in R or create a list from vector can be done by using as.list() or list() functions. A Vector in R is a basic data structure consist elements of the same data type whereas an R List is an object consisting of heterogeneous elements meaning can contain elements of different types.

Both these list() and as.list() functions take input as a vector and convert it to a list. In this article, I will explain both these method syntaxes and usage with examples. Note that almost all lists in R are internally referred as Generic Vectors,

1. Quick Examples of Convert Vector to List

Following are quick examples of converting a vector to a list or creating a list from the vector.


# Quick Examples

# Convert vector to list
x <- c("1","2")
li <- list(x)
print(li)
typeof(li)

# Convert vector to list
x <- c("1","2")
li <- as.list(x)
print(li)
typeof(li)

# Convert multiple vectors into list
x <- c("1","2")
x1 <- c(4:6)
li <- list(c(x,x1))
print(li)
typeof(li)

# Convert multiple vectors into list
x <- c("1","2")
x1 <- c(4:6)
li <- list(x,x1)
print(li)
typeof(li)

# Another example
x<-list("X","Y")
x1<-as.list(1:3)
li<-c(x,x1)
print(li) 

2. Using list() to Create Vector from List

To create a vector use c() function and to create a list use list() function. list() can also be used to create a list from a vector by taking the vector as an argument. In the following example, I have converted a single dimension vector into a list by using the list() function.


# Create list() from vector
x <- c("1","2")
li <- list(x)
print(li)

Yields below output.


# Output
> print(li)
[[1]]
[1] "1" "2"

You can check the type by using typeof() function.


# Type
typeof(li)

# Output
# >typeof(li)
#[1] "list"

3. Using as.list() to Convert Vector to List

The as.list() function is used to convert the vector to a list in R, This function takes syntax as.list(x, …) where x is the input object, in our case we will use a vector.

This as.list() function can also be used to convert other data objects in R such as data frames, factors, matrices, etc to a list object.


# Convert vector to list
x <- c("1","2")
li <- as.list(x)
print(li)
typeof(li)

Yields below output.


# Output
> print(li)
[[1]]
[1] "1"

[[2]]
[1] "2"

> typeof(li)
[1] "list"

4. Convert Multiple Vectors into List

By using the functions explained above we can also convert multiple vectors into the list in R. For example when using the list() function, pass both vectors as an argument in order to convert them into the list.


# Convert multiple vectors into list
x <- c("1","2")
x1 <- c(4:6)
li <- list(c(x,x1))
print(li)
typeof(li)

Yields below output.


# Output
> print(li)
[[1]]
[1] "1" "2" "4" "5" "6"

> typeof(li)
[1] "list"

If you notice it combines elements of both vectors into a single list, If you want elements of both vectors in separate lists use the below.


#Convert multiple vectors into list
x <- c("1","2")
x1 <- c(4:6)
li <- list(x,x1)
print(li)
typeof(li)

Yields below output.


# Output
> print(li)
[[1]]
[1] "1" "2"

[[2]]
[1] 4 5 6

> typeof(li)
[1] "list"

Conclusion

In this article, I have explained how to create a list from a vector or convert a vector into a list by using list() and as.list() functions. Also learned how to convert multiple vectors into list objects by using these functions. You can find the complete example from this article at Github R Programming Examples Project.

References

Related Articles

convert vector list

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing How to Convert Vector to List in R