Last active
August 29, 2015 14:10
-
-
Save Oreotrephes/35e7052d54f97c7eca0f to your computer and use it in GitHub Desktop.
vectorization fail
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
| summaryfunction<-function(x){ | |
| a<-if(is.na(x)){"NA"} else if(x<0){"-"} else {"+"} | |
| b<-if(is.na(x)){"NA"} else if(abs(x)<0.001){"***"} else if(abs(x)<0.01){"**"} else if(abs(x)<0.05){"*"} else{" "} | |
| paste(a,b) | |
| } | |
| #individually are fine | |
| summaryfunction(listy[1]) | |
| summaryfunction(listy[2]) | |
| #but together breaks | |
| listy<-c(0.005,-0.049) | |
| summaryfunction(listy) | |
| #mapply makes it work | |
| mapply(summaryfunction,output) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
summaryfunction2<-function(x,y){
signs<-substr(as.character(sign(x)),1,1)
signs[signs=="1"]<-"+"
significance<-cut(abs(y), c(0, 0.001, 0.01, 0.05, Inf), labels=c("**", "__", "", ""), right=FALSE)
paste(signs, significance)
}
datas1<-c(1,2,3)
datas2<-c(0.005,0.05,0.04)
summaryfunction(datas)