I'd like to melt this:
test = list( one = "joe" , two = c( "john" , "jane" ) )
Into a character vector:
c( "joe" , "john" , "jane" )
I tried melt() in the reshape package, but that results in a data.frame where strings are treated as factors, so I'd have to do something like:
as.character( melt( test )$value )
Is there a shorter/faster way?
To convert a list to a vector in R use unlist() function. This function takes a list as one of the arguments and returns a Vector.
A list holds different data such as Numeric, Character, logical, etc. Vector stores elements of the same type or converts implicitly. Lists are recursive, whereas vector is not. The vector is one-dimensional, whereas the list is a multidimensional object.
Convert List to DataFrame using data. data. frame() is used to create a DataFrame in R that takes a list, vector, array, etc as arguments, Hence, we can pass a created list to the data. frame() function to convert list to DataFrame. It will store the elements in a single row in the DataFrame.
unlist() function in R takes a list as an argument and returns a vector. A list in R contains heterogeneous elements meaning can contain elements of different types whereas a vector in R is a basic data structure containing elements of the same data type.
unlist(test)
(my answer needs to be more than 30 characters!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With