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

Somewhat unexpected "invalid subscript type 'double'" when using $ #1554

Closed
chrislad opened this issue Nov 26, 2015 · 5 comments
Closed

Somewhat unexpected "invalid subscript type 'double'" when using $ #1554

chrislad opened this issue Nov 26, 2015 · 5 comments

Comments

@chrislad
Copy link

Dear all,

I wanted to add a new column to a data frame with a constant value from a list:

someVariable <- list(a = 3, b = 5)
dplyr::mutate(mtcars, newColumn = someVariable$a) %>% head

This works.

But if a variable of the same name ("a") is present, I get an error:

someVariable <- list(a = 3, b = 5)
a <- 10
dplyr::mutate(mtcars, newColumn = someVariable$a) %>% head
Error: invalid subscript type 'double'

A fix consists in encapsulating the offending call with I():

someVariable <- list(a = 3, b = 5)
a <- 10
dplyr::mutate(mtcars, newColumn = I(someVariable$a)) %>% head()

(edit: this fix might not be advisable as newColumn is of class AsIs and not of the expected double. To avoid the issue, simply store the value into a new variable and use this new variable instead of someVariable$a in the call to dplyr::mutate.)

This is reminiscent of bug #421 , which is closed.

Whether it's something to fix or not, I leave it up to you but it was a bit maddening to debug (in my case, the variable 'a' was only sometimes present in the global environment, so my tests were passing when run from a separate environment, but not always when run from the console.).

thanks.

@CameronBieganek
Copy link

I've been having this same issue also, and I agree that the problem is hard to debug at first, before you realize what's going on. Here's a stack overflow post I made before seeing this bug report:

http://stackoverflow.com/questions/34054968/mutate-is-trying-to-extract-using-the-value-of-a-global-variable-when-using-th

I've also had this issue at least once in the past when I was trying to extract a function from an object, and the function happened to have the same name as a different function on the search path (while doing this inside a mutate).

@chrislad
Copy link
Author

chrislad commented Dec 4, 2015

Interesting. Also, the thread suggests a good workaround by quoting the field name:

someVariable <- list(a = 3, b = 5)
a <- 10
dplyr::mutate(mtcars, newColumn = someVariable$'a') %>% head

@danielsjf
Copy link

@LeeMendelowitz
Copy link
Contributor

I just got bit by this issue as well.

@hadley
Copy link
Member

hadley commented Mar 1, 2016

Duplicate of #1400

@hadley hadley closed this as completed Mar 1, 2016
bpbond added a commit to bpbond/cpcrw_incubation that referenced this issue Mar 29, 2016
When ‘y’ was present global environment, an error (“invalid subscript
type 'double’”) would be generated by dplyr. See
tidyverse/dplyr#1554.
@lock lock bot locked as resolved and limited conversation to collaborators Jun 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants