0

I've got a list that is the result of the following command:

lapply(data$displayDate, function(x) as.Date(as.POSIXct(x, origin = "1970-01-01")))

That comes out looking like this:

> data$displayDateDay[1:10]
[[1]]
[1] "2015-05-02"

[[2]]
[1] "2015-05-10"

[[3]]
[1] "2015-05-10"

[[4]]
[1] "2015-05-10"

[[5]]
[1] "2015-05-11"

[[6]]
[1] "2015-05-12"

[[7]]
[1] "2015-05-12"

[[8]]
[1] "2015-05-13"

[[9]]
[1] "2015-05-13"

[[10]]
[1] "2015-05-26"

This is all well and good but I can't figure out how to get this back into a property column vector in my data table. If I try to unlist this list, here's what I get:

> unlist(testVector)
 [1] 16557 16565 16565 16565 16566 16567 16567 16568 16568 16581

The class is numeric, whereas I wanted a date. Why is this happening, and how can I achieve a vector of dates?

helloB
  • 3,472
  • 10
  • 40
  • 87

2 Answers2

2

Use do.call():

list <- lapply(data$displayDate, function(x) as.Date(as.POSIXct(x, origin = "1970-01-01")))
vec <- do.call("c", list)

By the way, a quick search through the SO database would have turned up this gem.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    Thanks for the tip and the link to the prior post. I clearly didn't use the right search terms. – helloB Feb 24 '16 at 00:43
0

You didn't really need the lapply first place...

If data is a data frame, the following should give you a column in the form you want:

data <- as.data.frame(cbind(row = c(1, 2, 3), displayDate = c(345234523, 242345553, 302424556)))

> data
  row displayDate
1   1   345234523
2   2   242345553
3   3   302424556

data$displayDateDay <- as.Date(as.POSIXct(data$displayDate, origin = "1970-01-01"))

> data
  row displayDate displayDateDay
1   1   345234523     1980-12-09
2   2   242345553     1977-09-05
3   3   302424556     1979-08-02
retrography
  • 6,302
  • 3
  • 22
  • 32