I incidentally found a surprising behavior for $
operator when selecting columns in data frames:
gg <- data.frame(faa = 1:10, gaa = rep(c("x", "y"), 5))
gg$f
# [1] 1 2 3 4 5 6 7 8 9 10
I always thought that you have to use exact column name for $
too, maybe because that is how it works for "["
. It seems that $
operator guesses the rest of the column name because there are no other columns starting with f.
ggg <- data.frame(faa = 1:10, gaa = rep(c("x", "y"), 5), faagaa = 1:10)
ggg$f
# NULL
ggg$faag
# [1] 1 2 3 4 5 6 7 8 9 10
- Has this behavior been part of R functionality for a long time (because I have not experienced it before) or is it something relatively new?
- How does it work? I.e. what is the mechanism behind this behavior?
- How could one use this as an advantage when working with R? (other cases where $ works in a similar way)