Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn R list of dates into vector

Tags:

r

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!

like image 326
Carlos Avatar asked Jun 03 '14 12:06

Carlos


People also ask

How do I turn a list into a vector?

To convert List to Vector in R, use the unlist() function. The unlist() function simplifies to produce a vector by preserving all atomic components.

Can you have a vector of lists in R?

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.

What does unlist () do 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.

How do I convert a character vector to a date vector in R?

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.


1 Answers

To preserve the Date class, if your list is called d you can use do.call(c, d).

like image 134
konvas Avatar answered Nov 15 '22 19:11

konvas