1
> z <- data.table(x = 1:5, y = 1:5)
> z[["p"]]
NULL
> z[, c("p")]
Error in `[.data.table`(z, , c("p")) : column(s) not found: p

z[["p"]] does not give error unlike z[, c("p")]. I do understand the latter needs to return a data.table while first one returns a vector. A possible reason seems to be x[, j] format needs to support multiple columns, and hence it throws error pointing out the issue columns, if any. Can someone confirm this ?

cryptickey
  • 169
  • 8
  • 1
    Related post: https://stackoverflow.com/q/1169456/680068 – zx8754 Apr 14 '21 at 12:08
  • 1
    This isn't just `data.table`, it's things that inherit `data.frame` (including `tbl_df`). `$` and `[[` are very similar, `z$p` will also return `NULL`. For whatever reason (idk), the default behavior of `[[` is to be more tolerant of not-present fields. – r2evans Apr 14 '21 at 16:20

0 Answers0