I have a list of Dates which is returned by a function and looks like the following:
$`2014-03-02T05:20:13,2014-03-02T05:20:13`
[1] "2014-03-02" "2014-03-02"
$`2013-12-04T21:06:28,2013-12-04T21:06:37,2013-12-04T21:06:46,2013-12-04T21:06:49,2013-12-04T21:07:16,2014-01-09T16:20:55,2014-01-09T18:20:59,2014-01-09T18:21:08,2014-01-09T18:21:08`
[1] "2013-12-04" "2013-12-04" "2013-12-04"
[4] "2013-12-04" "2013-12-04" "2014-01-09"
[7] "2014-01-09" "2014-01-09" "2014-01-09"
I would like to put all the individual elements of the list into a vector of Dates. What is the best way to accomplish that?
Thanks!
To convert List to Vector in R, use the unlist() function. The unlist() function simplifies to produce a vector by preserving all atomic components.
Almost all data in R is stored in a vector, or even a vector of vectors. A list is a recursive vector: a vector that can contain another vector or list in each of its elements. Lists are one of the most flexible data structures in R.
unlist() Usage Use unlist() function to convert a list to a vector by unlisting the elements from a list. 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. Let's convert the simple list.
You can use the as. Date( ) function to convert character data to dates. The format is as. Date(x, "format"), where x is the character data and format gives the appropriate format.
To preserve the Date class, if your list is called d
you can use do.call(c, d)
.
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