Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow indexing by factors #23

Open
richierocks opened this issue Jun 16, 2017 · 0 comments
Open

Disallow indexing by factors #23

richierocks opened this issue Jun 16, 2017 · 0 comments

Comments

@richierocks
Copy link

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 d
x[as.character(index)]
##  a  b  c  d
## 16  9  4  1
x[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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant