2018-12-05
閱讀量:
863
R循環(huán)語句2
### for循環(huán)與算法
test.sum <- function(x)
{
res <- 0 ### 設(shè)置初始值,在第一次循環(huán)的時候使用
for(i in 1:length(x)){
res <- res + x ## 這部分是算法的核心,
##總是總右面開始計算,結(jié)果存到左邊的對象
}
return(res)
}
### 檢驗函數(shù)
a <- c(1,2,1,6,1,8,9,8)
test.sum(a)
sum(a)
無論是什么樣的函數(shù),算法才是最關(guān)鍵的。往往需要巧妙得設(shè)計算法,讓函數(shù)快捷高效。






評論(0)


暫無數(shù)據(jù)
推薦帖子
0條評論
0條評論
0條評論