3

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?

anu
  • 295
  • 2
  • 7
  • 15
  • 1
    Like @LostBrit said, you need to share a data sample. Please see this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – asb Jul 19 '13 at 06:26
  • 1
    `lapply` returns a list. Using `c` on two list returns a list. There is no `as.POSIXct` method for lists. – Roland Jul 19 '13 at 07:04

1 Answers1

4

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 :)

Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
  • Thank you for your help but this is also showing the same error. My G is: `[[1]] event_id time_occurred [1,] "MX050113" "2013-04-01 20:27:48" [2,] "MX050113" "2013-04-01 20:27:18"` – anu Jul 19 '13 at 06:01
  • 1
    @anu, this doesn't help me. You need to look at this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Andy Clifton Jul 19 '13 at 15:18