R coding hints

1, If you do not know how R internally convert the various data types, declear your variable name as explicitly as you can(variable type, character or numeric or boolen). see an example:
   let's say that you need a matrix data structure to store something(after a series of computation using "for" loop), usually,i done this by two means:
   I: state a vector(of whatever type,char, numeric etc) named mat_A, get a vector, vec_i, in every loop body and bind mat_A with vec_i to generate the matrix
   II:calculate the ncol and nrow of the matrix in advance, generate the matrix mat_A using matrix(ncol=m,nrow=n)->mat_A; set related part of the matrix in every loop body.
   The second method is strongly recommended. because you may make numeric data totally(and automatically) converted to character data without intention using the first method and this kind of error is hard to debug because you do not know much about that.

2, some function have specific behavior, be aware of and pay attention to these behavior.
   take the function aes(in ggplot2 package) as an example.
   aes map related value to the aresmatic operation. func info: aes(x ,y, ....more variables)
   what i want to put emphasis on is its behavor: aes look for two places to map x y variables, one is the global enviroment, the other is the
   dataframe(colnames(df)) u provide to him. So, there will be problems when you enclosure this tiny function in your own function without proper prcession(didn't pass him the dataframe u want to tackle or unproperly place the variable in global enviroment in ahead). example code is as follows:

colscatterplot<-function(commonExp){
    require(ggplot2)
    commonExp
<-as.data.frame(commonExp)
    #every possible combinations
    cmbn
<-combn(ncol(commonExp),2)
    #cat(ncol(cmbn),
" ",nrow(cmbn),"\n")
    
for(i in 1: ncol(cmbn))
    {
        colnma
<-colnames(commonExp)[cmbn[,i][1]]
        cat(colnma,
"\n")
        colnmb
<-colnames(commonExp)[cmbn[,i][2]]
        cat(colnmb,
"\n")
        m 
= lm(as.formula(paste(colnmb ,colnma,sep="~")), commonExp)
        eq 
<- substitute(italic(y)==b%*%italic(x)+a*~~italic(r)^2==r2,list(a = format(coef(m)[1], digits = 2), b = format(coef(m)[2], digits = 2),r2 = format(summary(m)$r.squared, digits = 3)))
       
eqlbel<-as.character(as.expression(eq))
        fnm
<-paste(colnma,"-",colnmb,".png",sep="")
        cat(fnm,
"\n")
        png(filename
=fnm)
        p.obj
<-ggplot(data=commonExp, aes_string(x=colnma,y=colnmb)) + geom_point() + geom_smooth(method=lm) + geom_text(aes(label=eqlbel) parse = TRUE)
        print(p.obj)
        #ggsave(filename
=fnm,plot=p.obj)
        dev.off()
    }
    }
   the funtion above will not act properly, as aes in gem_text will look for a global variable named eqlbel and assigns not the local eqlabel in this function but the golbal one to its label param, if there isn't a global variabe named eqlbel,the function exit with an error,but it will become a magic bug if there happen to be a variable named eqlbel. this kind of error can trap your debugging procedure for a long time without any progression.

posted on 2012-07-22 20:39 ewre 阅读(280) 评论(0)  编辑 收藏 引用 所属分类: R


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2012年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

留言簿(2)

文章分类

文章档案

最新评论

阅读排行榜