R: 自定义函数

###################################################

问题:自定义函数   18.4.29

         自定义函数,其返回值是个问题,还有怎么让过程变量成为全局变量,是个问题????。。。。。。。。。。。。。

解决方案:

         NA.detec <- function(x){

           D = sum(is.na(x),na.rm = TRUE)}

         ZERO.detec <- function(x){

           # D = colSums(x == 0,na.rm = TRUE)  #in colSums(x == 0, na.rm = TRUE) :'x' must be an array of at least two dimensions

           D = sum(x == 0,na.rm = TRUE)}

         NA.ZERO.detec <- function(x,choice = "NA"){

           if (choice=="NA"){

                   D = sum(is.na(x),na.rm = TRUE)

                   print(D)

           }else if(choice=="ZERO"){

                   D = sum(x == 0,na.rm = TRUE)

                   print(D)

           }else{print("have no this method.")}

         } 

         do.call("NA.ZERO.detec",args = list(liang$leave_time,"ZERO"))  #调用函数名,后面为参数。

         #函数内部找不到某个变量时,直接在外部寻找 使用。所以不能重名。。

         #函数的最后一行的结果就是返回信息。

讨论扩展:

        

另请参阅:

猜你喜欢

转载自www.cnblogs.com/li-20151130/p/9038408.html