I have a simple R data.frame
object df
. I am trying to select rows from this dataframe based on logical indexing from a column col
in df
.
I am coming from the python world where during similar operations i can either choose to select using df[df[col] == 1]
or df[df.col == 1]
with the same end result.
However, in the R data frame df[df$col == 1]
gives an incorrect result compared to df[df[,col] == 1]
(confirmed by summary
command). I am not able to understand this difference as from links like http://adv-r.had.co.nz/Subsetting.html it seems that either way is ok. Also, str
command on df$col
and df[, col]
shows the same output.
Is there any guidelines about when to use $
vs []
operator ?
Edit: digging a little deeper and using this question as reference, it seems like the following code works correctly
df[which(df$col == 1), ]
however, not clear how to guard against NA
and when to use which