Created
April 1, 2014 10:14
-
-
Save otsaw/9911389 to your computer and use it in GitHub Desktop.
Testing type promotions with dplyr's rbind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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