You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since factors sometimes behave like character vectors and sometimes behave like integers, it is a bad idea to pass a factor into a square bracket index. For example
(x<- c(d=1, c=4, b=9, a=16))
## d c b a## 1 4 9 16
(index<-factor(letters[1:4]))
## [1] a b c d## Levels: a b c dx[as.character(index)]
## a b c d## 16 9 4 1x[as.integer(index)]
## d c b a## 1 4 9 16
Can you guess what x[index] returns?
If it took some thinking about, then I think you'll agree that factor indexing is problematic.
It would be useful if strict mode caused x[index] to throw an error.
The text was updated successfully, but these errors were encountered:
Since factors sometimes behave like character vectors and sometimes behave like integers, it is a bad idea to pass a factor into a square bracket index. For example
Can you guess what
x[index]
returns?If it took some thinking about, then I think you'll agree that factor indexing is problematic.
It would be useful if strict mode caused
x[index]
to throw an error.The text was updated successfully, but these errors were encountered: