Open In App

Convert list to array in R

A list can be converted to array in R by calling unlist( ) function with created list as parameter. Now pass the unlist() function into array() function as parameter, and use dim attribute for specifying number of rows, columns and matrices. unlist() converts list to vector.

 Syntax : 



array( unlist( list_variable ) , dim = ( rows ,columns , matrices ))

Given below are various implementation for the same:



Example 1 :




lst1=list(1:10)
 
arr=array(unlist(lst1),dim=c(3,3,3))
print(arr)

Output :

Example 2 :




lst1=list(c("karthik","nikhil","sravan"))
 
arr=array(unlist(lst1),dim=c(1,3,3))
print(arr)

Output:

Example 3 :




lst1=list(c("karthik","chandu","nandu"))
 
arr=array(unlist(lst1),dim=c(1,3,2))
print(arr)

Output:


Article Tags :