-2

I was trying to calculate the following sum in R and I get stuck because I can't find the right sum function in R. Any help will be appreciated.

Sum(i, {i,1,10})

The pseudo code for this can be as follows:

for(i=1;i<=10;i++)
    sum=i;
Nasi Jofce
  • 11
  • 6
  • 1
    It would be great if you could supply a minimal reproducible example to go along with your question. Something we can work from, something that show your "more complex sum," and use to show you how it might be possible to answer your question. That way others can also befit form your question, and the accompanying answer, in the future. You can have a look at [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. – Eric Fail Jan 09 '16 at 20:05
  • @EricFail I provided the picture of the sum. – Nasi Jofce Jan 09 '16 at 20:08
  • @RichardScriven I am using the following code and I get an error message: rezult<-c(1:30) n<-20 k<-5 for(a in 1:30){ sum<-0 for(i in 0:k-1){ for(j in 0:n-1){ sum<-sum+power(-1,j)*choose(n-1,j)*power((n-j-1)/n,a*k-1) } } result[a]<-sum } – Nasi Jofce Jan 09 '16 at 20:17
  • for calculaing sum of 1-10 output is 55 – varunkumar Jan 09 '16 at 20:59

2 Answers2

1

You should check out the docs (?sum in RStudio) but here are some things you can do with sum

mynumbers <- c(1:5, 13, 17, pi, factorial(5))
sum(mynumbers)
#[1] 168.1416
mynumbers2 <- c(1:5, 13, 17, NA, NA, pi, factorial(5))
sum(mynumbers2, na.rm = TRUE)
#[1] 168.1416
steveb
  • 5,382
  • 2
  • 27
  • 36
0

Maybe this could help:

sum(1:10)
milos.ai
  • 3,882
  • 7
  • 31
  • 33
  • It helps, but I need it to calculate a more complex sum which has combinations and factorials. I was looking for the relevant function in r. – Nasi Jofce Jan 09 '16 at 20:02
  • 4
    maybe you can post that more complex example here? – milos.ai Jan 09 '16 at 20:04
  • This is the sum I was trying to find but I don't get the right results. Any suggestion? result<-c(1:30) n<-20 k<-5 for(a in 1:30){ sum<-0 for(i in 0:k-1){ for(j in 0:n-1){ sum<-sum+(-1^j)*choose(n-1,j)*(((n-j-1)/n)^ (a*k-1)) } } result[a]<-sum } – Nasi Jofce Jan 09 '16 at 20:26