Reputation: 2788
I am trying to break up a for-loop into a tibble then use the summarize()
function to get the final value.
This is the loop
prob <- NULL
for(i in(4:16)){
p1 = dbinom(i,16,0.2)
k=13-i
p2=sum(dbinom(k:30,30,0.2))
prob = c(prob,p1*p2)
print(i)
}
I got this far
df <- tibble( i=4:16) %>%
mutate(p1=dbinom(i,16,0.2),k=as.integer(13-i)) %>%
mutate(p2=sum(dbinom(k:30,30,0.2)))
df
this produces the following error
Warning message:
In k:30 : numerical expression has 13 elements: only the first used
The warning says the k only has 13 elements, but this should not be a problem because the rest of my data has 13 elements. I am not sure how to fix the problem.
Upvotes: 0
Views: 3508
Reputation: 1540
you should manipulate data by row: try this
tibble( i=4:16) %>%
mutate(p1=dbinom(i,16,0.2),k=as.integer(13-i)) %>%
rowwise() %>%
mutate(p2=sum(dbinom(k:30,30,0.2)))
Upvotes: 3