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

is.null & is.na are missing parentheses #2302

Closed
JohnMount opened this issue Dec 9, 2016 · 2 comments
Closed

is.null & is.na are missing parentheses #2302

JohnMount opened this issue Dec 9, 2016 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@JohnMount
Copy link

Using the RSQLite dplyr adaption of tbl dplyr::mutate I am seeing previous values of the left-hand side of assignments lost in certain situations. I think it has to do with types (int/double/logical) causing some confusion.

Example below (and here):

library('dplyr')
d <- data.frame(x=c(1,2,2),y=c(3,5,NA),z=c(NA,'a','b'),
                rowNum=1:3,
                stringsAsFactors = FALSE)
print(d)

fnam <- tempfile(pattern = "dplyr_doc_narm", tmpdir = tempdir(), fileext = "sqlite3")
my_db <- dplyr::src_sqlite(fnam, create = TRUE)
class(my_db)
dRemote <- copy_to(my_db,d,'d',rowNumberColumn='rowNum',overwrite=TRUE)


# correct calculation
dRemote %>% mutate(nna=0) %>%
  mutate(nna=nna+ifelse(is.na(x),1,0)) %>% 
  mutate(nna=nna+ifelse(is.na(y),1,0)) %>% 
  mutate(nna=nna+ifelse(is.na(z),1,0))  
 
# incorrect calculation (last step seems to always clobber the previous result)
dRemote %>% mutate(nna=0) %>%
  mutate(nna=nna+is.na(x)) %>% 
  mutate(nna=nna+is.na(y)) %>% 
  mutate(nna=nna+is.na(z))

# clean up
rm(list=setdiff(ls(),'fnam'))
if(!is.null(fnam)) {
  file.remove(fnam)
}
gc()
@krlmlr
Copy link
Member

krlmlr commented Dec 9, 2016

Thanks, confirmed. Need to add parens in the SQL rendering of is.null() and is.na(). Would you like to contribute a fix?

@krlmlr krlmlr added bug an unexpected problem or unintended behavior database labels Dec 9, 2016
@JohnMount
Copy link
Author

JohnMount commented Dec 10, 2016

krlmlr, I'd be thrilled to. Your diagnosis plus dplyr::explain made the problem very clear. My pull request is here: #2307 .

Update: It will be better if somebody else patches this. Sorry about the missteps on my part.

@hadley hadley changed the title RSQLite dplyr adaption of tbl dplyr::mutate loses left hand side of assignment is.null & is.na are missing parentheses Feb 14, 2017
@hadley hadley closed this as completed in 2e23ac4 Feb 14, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants