'asfn(rs[[i]])':need explicit units for numeric conversion

Hi, I'm having trouble doing this in R:

table$days <- difftime(as.Date("2020-12-31"),as.Date(table$data_od), units = "days")

spr<-sqldf("select days from table")

Error: 'asfn(rs[[i]])':need explicit units for numeric conversion

How can I solve this problem?

Hi @Emy1
You don't need to resort to sql-type functions to access the days column from a dataframe:

# Make some dummy input data
# Try to avoid giving objects the same name as a function ("table")
my_table <- data.frame(data_od = seq.Date(from=as.Date("2020-02-15"), 
                                          to=as.Date("2020-02-20"),
                                          by="days"))
my_table
#>      data_od
#> 1 2020-02-15
#> 2 2020-02-16
#> 3 2020-02-17
#> 4 2020-02-18
#> 5 2020-02-19
#> 6 2020-02-20

my_table$days <- difftime(as.Date("2020-12-31"), as.Date(my_table$data_od), units = "days")
my_table
#>      data_od     days
#> 1 2020-02-15 320 days
#> 2 2020-02-16 319 days
#> 3 2020-02-17 318 days
#> 4 2020-02-18 317 days
#> 5 2020-02-19 316 days
#> 6 2020-02-20 315 days

#spr <- sqldf("select days from table")
spr <- as.numeric(my_table$days)
spr
#> [1] 320 319 318 317 316 315

Created on 2021-09-11 by the reprex package (v2.0.1)
HTH

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.