Skip to content

Instantly share code, notes, and snippets.

@otsaw
Created April 1, 2014 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otsaw/9911389 to your computer and use it in GitHub Desktop.
Save otsaw/9911389 to your computer and use it in GitHub Desktop.
Testing type promotions with dplyr's rbind
.rbind = function(x, y)
tryCatch({
dplyr::rbind_list(x, y)
message("OK")
}, error=function(e) print(e))
message("logical --> integer")
.rbind(data.frame(x=TRUE), data.frame(x=1L))
.rbind(data.frame(x=1L), data.frame(x=TRUE))
message("logical --> numeric")
.rbind(data.frame(x=TRUE), data.frame(x=1.5))
.rbind(data.frame(x=1.5), data.frame(x=TRUE))
message("integer --> numeric")
.rbind(data.frame(x=1L), data.frame(x=1.5))
.rbind(data.frame(x=1.5), data.frame(x=1L))
# Not sure if factor should be promoted to character or character to factor.
# base::rbind seems to keep type of the first argument.
message("factor <--> character")
.rbind(data.frame(x="foo"), data.frame(x="foo", stringsAsFactors=FALSE))
.rbind(data.frame(x="foo", stringsAsFactors=FALSE), data.frame(x="foo"))
message("NA --> integer")
.rbind(data.frame(x=NA), data.frame(x=1L))
.rbind(data.frame(x=1L), data.frame(x=NA))
message("NA --> factor")
.rbind(data.frame(x=NA), data.frame(x="foo"))
.rbind(data.frame(x="foo"), data.frame(x=NA))
message("NA --> numeric")
.rbind(data.frame(x=NA), data.frame(x=1.5))
.rbind(data.frame(x=1.5), data.frame(x=NA))
message("NA --> character")
.rbind(data.frame(x=NA), data.frame(x="foo", stringsAsFactors=FALSE))
.rbind(data.frame(x="foo", stringsAsFactors=FALSE), data.frame(x=NA))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment