Skip to content

Instantly share code, notes, and snippets.

@Akiyah
Created November 6, 2014 03:22
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 Akiyah/deec63e776b23322066d to your computer and use it in GitHub Desktop.
Save Akiyah/deec63e776b23322066d to your computer and use it in GitHub Desktop.
override operator "+" in R
"+" <- function(e1, e2=NA) {
if (is.character(e1) & is.character(e2)) {
return(paste0(e1, e2))
}
ifelse(is.na(e2), base::"+"(e1), base::"+"(e1, e2))
}
1+2+3
#=> [1] 6
"f"+"g"+"h"
#=> [1] "fgh"
+2
#=> [1] 2
c(1,2,3)+c(4,5,6)
#=> [1] 5 7 9
c("1","2","3")+c("4","5","6")
#=> [1] "14" "25" "36"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment