How do you perform such summation in R?
sum_{i=1}^3 (x^2)
i=1 is lower bound
i=3 is upper bound
x^2 is the operation
So we will perform
1^2 + 2^2 + 3^2
Using standard loop:
tot <-0
for (x in 1:3) {
tot <- tot + x^2
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, I’ll point out that to generate a vector containing the elements
1,2,3you can do:Secondly, R is a vectorised language – meaning if
xis a vector and I dox + 5it’ll add 5 to each element ofxfor me without needing a for loop.Thirdly, look at
?sum, wherebysum(x)adds up all the elements inx.