anu
anu

Reputation: 295

Time difference in R: POSIXct error

I am using this code to calculate time difference.

ctym <- lapply(G, function(x) x[k,2])
y <- lapply(G, function(x) x[j,2])
t1 <- c(y,ctym)
dt1 <- as.POSIXct(t1)
dtyme <- difftime(dt1[2],dt1[1],units = "mins")

Output for t1 is:

 [[1]]
      time_occurred 
"2013-04-01 20:27:18"

[[2]]
    time_occurred 
"2013-04-01 20:27:48"

Error I am getting is: Error in as.POSIXct.default(t1) : do not know how to convert 't1' to class “POSIXct”

What's wrong here?

Upvotes: 3

Views: 14853

Answers (1)

Andy Clifton
Andy Clifton

Reputation: 5066

t1 is not a POSIXct object. I think it might be a list at the moment. You'll need to do something like...

t1 <- as.POSIXct(unlist(t1), format = "%y-%m-%d H:M:S"))

.... to get t1 into the right format to do some maths.

This might not be required if you use paste() instead of c() to combine your data, but without your data we can't really tell.

If you could post some of your data we can see what we can do :)

Upvotes: 4

Related Questions