R: Usage of NULL
Peter Lin
2018/4/4
The first method:
min=function(x){
s=NULL
for (i in x){
if (is.null(s)) s=i else if (i<s) s=i
}
return (s)
}
min(c(3,6,3,2))
## [1] 2
The second method:
Be careful about or logical, using ‘||’ not ‘|’
min2=function(x){
s=NULL
for (i in x){
if (is.null(s) || i<s) {s=i}
}
return (s)}
min2(c(13,6,5,21))
## [1] 5
沒有留言:
張貼留言