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

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.

主站蜘蛛池模板: 河北省| 开封县| 德兴市| 临邑县| 柯坪县| 乌拉特前旗| 成安县| 汶上县| 永年县| 霍州市| 铁岭县| 星子县| 梅河口市| 漯河市| 页游| 红桥区| 都兰县| 元谋县| 余姚市| 信阳市| 喀喇沁旗| 邯郸市| 安福县| 贡觉县| 丰台区| 罗定市| 富裕县| 正镶白旗| 遂昌县| 包头市| 屏南县| 日土县| 淳化县| 隆化县| 丰台区| 花莲县| 乳山市| 通河县| 白水县| 合肥市| 望谟县|