unlist

unlist(x)
Unlist a list of vectors into a single vector. This function will also unlist all lists within the list as well, but this setting may be turned off via additional options.
  • x – Usually a list.

Example. Two examples are below. The first simple example takes a list of three vectors, each of length 1, and concatenates them. The second example takes a list where some of the items are themselves lists, and concatenates all vectors in the list tree.
> test1 <- list(5, "b", 12)
> unlist(test1)
[1] "5"  "b"  "12"
> 
> 
> test2 <- list(v1=5, v2=list(2983, 1890), v3=c(3, 119))
> test2
$v1
[1] 5

$v2
$v2[[1]]
[1] 2983

$v2[[2]]
[1] 1890


$v3
[1]   3 119

> 
> unlist(test2)
  v1  v21  v22  v31  v32 
   5 2983 1890    3  119 
> 
> mean(unlist(test2))
[1] 1000
> 
Tip. This post only touches on a specific feature of unlist, which can be applied to more diverse objects than those shown in the example and may, in some circumstances, return a more complex object.

Leave a Reply