I'm fairly new to R and currently trying to write a for loop to calculate and add to the currently existing dataset.
It's not exactly what I'm trying to do but the idea is as follows.
R code below:
x1 = rnorm(n=100, m=0, sd=1)
x2 = rnorm(n=100, m=3, sd=2)
x3 = x1 * 4.12
d = data.frame(x1,x2,x3)
d$result[1] = (d[1,1] + d[1,2]) / d[1,3]
d$result[2] = (d[2,1] + d[2,2]) / d[2,3]
d$result[3] = (d[3,1] + d[3,2]) / d[3,3]
.
.
.
.
d$result[100] = (d[100,1] + d[100,2]) / d[100,3]
And I'm fully aware that I could add a result variable by simply applying
d$result = (x1 + x2) / x3
But as mentioned, as this isn't what I'm currently trying to, it'd be much appreciated if someone could please help me write the for loop mentioned above.
Many thanks in advance.
d$result = (x1 + x2) / x3may not fulfill your need? Otherwise the for-loop-version solution could befor (i in 1:100) {d$result[i] = (d[i,1] + d[i,2]) / d[i,3]}.set.seedgiven that it employs random numbers.