I'm trying to run my data frame through a loop to execute a function on each row and update the columns est, ll, and ul with the results. My data frame is onch. The loop seems to be working except the results for est, ll, and ul are the same for each row (presumably the last iteration). Any thoughts would be appreciated!
for (i in 1:nrow(onch)) {
row <- cbind(onch$c1, onch$c2, onch$c3)
pr1 <- removal(row)
a <- summary(pr1)
onch$est <- a[1]
b <- confint(pr1)
onch$ll <- b[1]
onch$ul <- b[2]
}
the data frame looks like this:
onch
site date c1 c2 c3 est ll ul
1 H1 7/11/12 6 2 1 NA NA NA
2 H2 7/15/12 12 4 0 NA NA NA
Thank you for the help! I still haven't solved the nrow copy issue, but this works:
for (i in 1:nrow(onch)) {
row <- cbind(onch$c1[i], onch$c2[i], onch$c3[i])
pr1<- removal(row)
a<- summary(pr1)
onch$est[i] <- a[1]
b <- confint(pr1)
onch$ll[i] <- b[1]
onch$ul[i] <- b[1,2]
}
i. So you're doing the same process invariant of row within theforloop, as there is noiinside the loop. Secondly, because R is assign-by-copy, you are needlessly creatingnrow(onch)copies of a data frame when you can probably create just one. Finally, what is theremovalfunction?removal?