Last active
August 29, 2015 14:11
-
-
Save jimhester/a48b250d3a0a7e699d86 to your computer and use it in GitHub Desktop.
More informative error messages for R
This file contains hidden or 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
| # missing variable | |
| `$` <- function(x, y) { var = deparse(substitute(y)); if(!deparse(substitute(y)) %in% names(x)) { stop('missing variable: ', var) }; x[[var]] } | |
| #> prob1<-as.data.frame(cbind(c(1,2,3),c(5,4,3))) | |
| #> colnames(prob1)<-c("Education","Ethnicity") | |
| #> table(prob1$Eduction, prob1$Ethnicity) | |
| # Error in prob1$Eduction : wrong name: Eduction | |
| # error when nrow is used on non data.frames or matrices | |
| nrow <- function(x) { if (is.data.frame(x) || is.matrix(x)) { dim(x)[1L] } else { stop("nrow cannot be used on ", type(x)) } } | |
| #> nrow(prob1$Education) | |
| # Error in nrow(prob1$Education) : nrow cannot be used on double |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is in response to http://rforpublichealth.blogspot.com/2013/01/translating-weird-r-errors.html