r - Accessing a non existent column of a data.table with dollar sign -
i using (probably outdated) version 1.10.4 of data.table r version 3.3.2 (2016-10-31). can access non-existent columns dollar sign. behaviour wanted?
code:
realoffloads = data.table(bag_tag = c(1,2,3)) realoffloads = realoffloads[, .(bag_tag, offload_real = t)] "offload" %in% names(realoffloads) x = realoffloads$offload although getting 'false' answer question whether or not column 'offload' exists getting out of (t,t,t) when accessing using dollar sign.
i using pretty in code little scared :-()
regards, fw
from base r documentation:
both
[[,$select single element of list. main difference$not allow computed indices, whereas[[does.x$nameequivalentx[["name", exact = false]]. also, partial matching behavior of[[can controlled using exact argument.
a data.table data.frame, , data.frame list, data.table list. inexact matching $ allowed. that's nice interactive session you're trying explore data, not nice non-interactive code can blow when unexpected happens.
this why it's almost always bad idea use $ subsetting if you're not 100% sure column exists. , then, fall prey typos. instead of dt$name, use dt[["name"]]. won't raise error, return null, easy check. if let go, null cause error down line.
Comments
Post a Comment