convert data.table difftime to integer

The code below will result in a data.table difftime object. I want to know how to convert it to an integer.

as.IDate("2001-01-05") - as.IDate("2001-01-01")

when I use the as. numeric() function in a data.table, I still get the difftime values in my newly created column.

Hi @Peter_Griffin,

Can you provide a reprex to show what you meand ?
Currently, I run your code I get an integer and don't need to convert... :thinking:

library(data.table)
x <- as.IDate("2001-01-05") - as.IDate("2001-01-01")
str(x)
#>  int 4
class(x)
#> [1] "integer"

Created on 2020-02-18 by the reprex package (v0.3.0.9001)

I may have miss something and a reproducible example would help. thank you

1 Like

Thanks for your reply. When I calculate the difference of two columns of IDate in a data.table, I get a column of difftime, something like 1 Day, 2 Day, etc.

However, when I try this code: as.IDate("2001-01-05") - as.IDate("2001-01-01"), I get an integer.

If try to create a reprex from what I understand here, I still get an integer column inside data.table

library(data.table)
dt <- data.table(x = as.IDate("2001-01-05"), y = as.IDate("2001-01-01"))
dt[, z := x -y]
dt
#>             x          y z
#> 1: 2001-01-05 2001-01-01 4
class(dt$x)
#> [1] "IDate" "Date"
class(dt$y)
#> [1] "IDate" "Date"
class(dt$z)
#> [1] "integer"

I obviously missed something.

Please, can you provide a reprex in order to help us help you ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.