After reading data.table
FAQ (section 1.5), I had an impression that all three ways of addressing the column are more or less equivalent. But at least the output of [, mycol, with=FALSE]
is quite different from $mycol
and [[mycol]]
:
dt1 <- fread(
" id,colA,colB
id1,3,xxx
id2,0,zzz
id3,NA,yyy
id4,0,aaa
")
dt1$colA <- factor(dt1$colA)
myvar="colA"
dt1$colA
# [1] 3 0 <NA> 0
# Levels: 0 3
dt1[[myvar]]
# [1] 3 0 <NA> 0
# Levels: 0 3
dt1[, myvar, with=FALSE]
# colA
# 1: 3
# 2: 0
# 3: NA
# 4: 0
So, what is exact difference between those three approaches? Can I assume that $mycol
and [[mycol]]
are always identical? Why [, mycol, with=FALSE]
"loses" factors?
Thanks in advance.