Toni - 4 days ago 4
R Question

Why does a loop in R starts at 0 as opposed to the first number in the sequence?

The output of the following loop starts at zero, and has a length of 76, while my intention was for the loop to produce 27 values. What am I doing wrong?

`````` p=0
for(i in 50:76){
p[i]= choose(76,i)*.5^76 }
p

[1] 0.000000e+00           NA           NA           NA
[5]           NA           NA           NA           NA
[9]           NA           NA           NA           NA
[13]           NA           NA           NA           NA
[17]           NA           NA           NA           NA
[21]           NA           NA           NA           NA
[25]           NA           NA           NA           NA
[29]           NA           NA           NA           NA
[33]           NA           NA           NA           NA
[37]           NA           NA           NA           NA
[41]           NA           NA           NA           NA
[45]           NA           NA           NA           NA
[49]           NA 2.034472e-03 1.037182e-03 4.986451e-04
[53] 2.258016e-04 9.617474e-05 3.846990e-05 1.442621e-05
[57] 5.061829e-06 1.658185e-06 5.058870e-07 1.433347e-07
[61] 3.759597e-08 9.095800e-09 2.021289e-09 4.105743e-10
[65] 7.579834e-11 1.263306e-11 1.885531e-12 2.495555e-13
[69] 2.893398e-14 2.893398e-15 2.445125e-16 1.698003e-17
[73] 9.304128e-19 3.771944e-20 1.005852e-21 1.323489e-23
``````

Answer

In general `for` loops are not preferred in R. Instead there is a family of functions, `apply` and its `xapply` descendants that are designed to iterate over objects similar to how other languages use loops but taking advantage of R's built in vectorization. You could use one, `sapply()`, here like this:

``````sapply(50:76, function(i) choose(76,i)*.5^76)
[1] 2.034472e-03 1.037182e-03 4.986451e-04 2.258016e-04 9.617474e-05 3.846990e-05 1.442621e-05 5.061829e-06 1.658185e-06
[10] 5.058870e-07 1.433347e-07 3.759597e-08 9.095800e-09 2.021289e-09 4.105743e-10 7.579834e-11 1.263306e-11 1.885531e-12
[19] 2.495555e-13 2.893398e-14 2.893398e-15 2.445125e-16 1.698003e-17 9.304128e-19 3.771944e-20 1.005852e-21 1.323489e-23
``````

If you really wanted to initalize a holding object beforehand @G5W is correct. You would initialize the object as `p <- c()` or `p <- numeric()` if you you wanted to specify the class at the same time.

If you want to keep your `for` loop intact you can't assign values based on index. By default index will fill all of the values from 1 until your first calculated value, here it is 50, with NA's. You could modify the structure to something this this:

``````p <- numeric()
for(i in 50:76){
j <- choose(76,i)*.5^76
p <- c(p, j)}
p
[1] 2.034472e-03 1.037182e-03 4.986451e-04 2.258016e-04 9.617474e-05 3.846990e-05 1.442621e-05 5.061829e-06 1.658185e-06
[10] 5.058870e-07 1.433347e-07 3.759597e-08 9.095800e-09 2.021289e-09 4.105743e-10 7.579834e-11 1.263306e-11 1.885531e-12
[19] 2.495555e-13 2.893398e-14 2.893398e-15 2.445125e-16 1.698003e-17 9.304128e-19 3.771944e-20 1.005852e-21 1.323489e-23
``````
Comments