Jeffrey Grueneberg
Jeffrey Grueneberg

Reputation: 33

How do I use the index value within the function of a for loop in R?

Multiplier <- numeric(200)
for (i in 1:200) Multiplier[i] <- 1/sqrt(i(i+1))

Like a math function where f(n)=1/sqrt(n(n+1), and put those first 200 values in an array. But when I run the above code I get:

# Error in i(i + 1) : could not find function "i" 

and when I try to use [i] I get:

# Error: unexpected '[' in "for (i in 1:200) Multiplier[i] <- 1/sqrt([

Upvotes: 0

Views: 24

Answers (1)

akrun
akrun

Reputation: 887128

Change the i(i+1) to i*(i+1) . When we use i() it is assuming i as function and the i+1 as argument to the function i

for (i in 1:200) Multiplier[i] <- 1/sqrt(i*(i+1))

-output

> Multiplier
  [1] 0.707106781 0.408248290 0.288675135 0.223606798 0.182574186 0.154303350 0.133630621 0.117851130 0.105409255 0.095346259
 [11] 0.087038828 0.080064077 0.074124932 0.069006556 0.064549722 0.060633906 0.057166195 0.054073807 0.051298918 0.048795004
 [21] 0.046524211 0.044455422 0.042562827 0.040824829 0.039223227 0.037742568 0.036369648 0.035093120 0.033903175 0.032791292
 [31] 0.031750032 0.030772873 0.029854072 0.028988552 0.028171808 0.027399831 0.026669037 0.025976217 0.025318484 0.024693240
 [41] 0.024098135 0.023531040 0.022990024 0.022473329 0.021979349 0.021506620 0.021053798 0.020619652 0.020203051 0.019802951
 [51] 0.019418391 0.019048483 0.018692405 0.018349396 0.018018749 0.017699808 0.017391962 0.017094641 0.016807316 0.016529490
 [61] 0.016260700 0.016000512 0.015748520 0.015504342 0.015267620 0.015038019 0.014815221 0.014598929 0.014388862 0.014184754
 [71] 0.013986356 0.013793431 0.013605757 0.013423121 0.013245324 0.013072175 0.012903494 0.012739112 0.012578865 0.012422600
 [81] 0.012270170 0.012121435 0.011976263 0.011834527 0.011696106 0.011560887 0.011428758 0.011299615 0.011173359 0.011049892
 [91] 0.010929125 0.010810969 0.010695340 0.010582159 0.010471348 0.010362833 0.010256545 0.010152415 0.010050378 0.009950372
[101] 0.009852336 0.009756214 0.009661948 0.009569488 0.009478779 0.009389775 0.009302426 0.009216688 0.009132515 0.009049866
[111] 0.008968700 0.008888977 0.008810658 0.008733708 0.008658090 0.008583770 0.008510715 0.008438894 0.008368274 0.008298827
[121] 0.008230522 0.008163333 0.008097232 0.008032193 0.007968191 0.007905200 0.007843198 0.007782160 0.007722065 0.007662891
[131] 0.007604618 0.007547224 0.007490689 0.007434996 0.007380124 0.007326056 0.007272775 0.007220264 0.007168505 0.007117483
[141] 0.007067182 0.007017587 0.006968683 0.006920457 0.006872893 0.006825978 0.006779700 0.006734045 0.006689001 0.006644555
[151] 0.006600696 0.006557412 0.006514693 0.006472526 0.006430901 0.006389809 0.006349238 0.006309180 0.006269623 0.006230560
[161] 0.006191980 0.006153875 0.006116237 0.006079055 0.006042324 0.006006033 0.005970176 0.005934744 0.005899731 0.005865128
[171] 0.005830929 0.005797126 0.005763713 0.005730683 0.005698029 0.005665745 0.005633825 0.005602263 0.005571052 0.005540187
[181] 0.005509663 0.005479473 0.005449612 0.005420074 0.005390855 0.005361950 0.005333352 0.005305058 0.005277063 0.005249362
[191] 0.005221950 0.005194823 0.005167976 0.005141405 0.005115106 0.005089075 0.005063307 0.005037799 0.005012547 0.004987547

According to ?Paren

Open parenthesis, (, and open brace, {, are .Primitive functions in R.

Effectively, ( is semantically equivalent to the identity function(x) x

Upvotes: 2

Related Questions