LandR icon indicating copy to clipboard operation
LandR copied to clipboard

avoid partial matching of data.table column names

Open achubaty opened this issue 6 years ago • 0 comments

using $ to extract columns is dangerous due to partial matching. the wrong columns may inadvertently be extracted.

> library(data.table)
> DT <- data.table(col1 = letters, col20 = LETTERS)
> DT$col2 
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"

Ensure that columns are extracted using data.table syntax ([["colName"]]):

> DT[["col2"]]
NULL
> DT[["col20"]]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"

achubaty avatar Jul 11 '19 05:07 achubaty