I have a vector
p <- seq(0,1,length=11)
That I want to use as the independent variable in the summation
If I craft a function that codes the summation by hand
f <- function(a){
a^0*(1-a)^5+a^1*(1-a)^4+...
}
and pass it p
, then I get the correct output and no errors are thrown
results <- f(p)
I can plot it, results
is the right length, everything's kosher. It just looks really ugly so I wanted to use sum()
instead and tried
i <- 0:5
g <- function(a,i){
sum(a^i*(1-a)^(5-i))
}
but when I attempt g(p,i)
it throws the error
longer object length is not a multiple of shorter object length
I believe the reason I'm getting this error is answered over here quite nicely, especially the part about recycling. sum
cycles through the i
vector as well as p
at the same time, then starts over with p[7]
and i[1]
when it runs into the end of i
. My question, however, is what is the CORRECT way to simplify f
into g
?