官术网_书友最值得收藏!

Basic loops in R

If we want to perform an action repeatedly in R, we can utilize the loop functionality.

How to do it…

The following lines of code multiply each element of x and y and store them as a vector z:

x = c(1:10)
y = c(1:10)
for(i in 1:10){
z[i] = x[i]*y[i]
}

How it works…

In the preceding code, a calculation is executed 10 times. R performs any calculation specified within {}. We are instructing R to multiply each element of x (using the x[i] notation) by each element in y and store the result in z.

主站蜘蛛池模板: 黄平县| 武汉市| 新沂市| 泰和县| 麻栗坡县| 黄石市| 马关县| 塔河县| 谷城县| 西吉县| 开封县| 阳朔县| 肥城市| 城固县| 黄山市| 慈利县| 军事| 潞城市| 巴彦淖尔市| 崇义县| 九龙城区| 毕节市| 凤台县| 阿拉善左旗| 湖南省| 清徐县| 陈巴尔虎旗| 丹江口市| 云浮市| 南丰县| 伊川县| 巴塘县| 喀什市| 汾西县| 莫力| 康平县| 广宗县| 广平县| 青浦区| 马公市| 乌兰浩特市|