0

I have a data.table with one column of POSIXcts.

dt <- data.table(DateTime=seq(from=as.POSIXct("2017-02-01 00:00", tz="Europe/Berlin"), by=3600, length.out = 10), Value=1:10)

I thought I can use max(), but that does not work.

maxDateTime <- max(dt[, "DateTime"])

And also last() from 'data.table' package is not doing it:

maxDateTime <- last(dt[, "DateTime"])

Only tail() is going in the right direction

maxDateTime <- tail(dt[, "DateTime"],1)

but returns a data.table, where I only would like to have a single value.

It is a rather simple problem, but I am stuck with it for a while now, so I hope for the right hint here. Thanks!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
schluk5
  • 177
  • 2
  • 13

1 Answers1

2

We need to use [[ to extract a single column

max(dt[["DateTime"]])

For multiple columns, the syntax is

dt[, c("DateTime", "Value"), with = FALSE]

Or another option is .

dt[, .(DateTime, Value)]
akrun
  • 874,273
  • 37
  • 540
  • 662