0

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]
 }
  • 2
    how does onch looks like (e.g., names(onch)) ?? – Gago-Silva Dec 26 '12 at 20:04
  • 1
    Can you share a sample of the data? For example using the principles explained at: http://meta.stackexchange.com/questions/155375/embedding-data-from-csv-file-in-line-of-code/155377#155377 – Jochem Dec 26 '12 at 20:06
  • The first problem I see is that you're not referencing anything with the `i`. So you're doing the same process invariant of row within the `for` loop, as there is no `i` inside the loop. Secondly, because R is assign-by-copy, you are needlessly creating `nrow(onch)` copies of a data frame when you can probably create just one. Finally, what is the `removal` function? – Blue Magister Dec 26 '12 at 20:10
  • @Jochem For samples/examples, I prefer [this question](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Blue Magister Dec 26 '12 at 20:12
  • What is the function `removal`? – Sven Hohenstein Dec 26 '12 at 20:16
  • The removal function is from the package FSA and is used to estimate fish abundance. Thank you for the comments. I will start by trying to incorporate the i into the loop, and will try to resolve the nrow(onch) copies issues. – yakamafish Dec 26 '12 at 20:19
  • For future reference, [documentation for `removal` at Rforge](https://www.rforge.net/doc/packages/FSA/removal.html). – Blue Magister Dec 26 '12 at 20:38

4 Answers4

1

Your line onch$ll <- b[1] overwrites the entire column ll of onch. To only update a specific entry, use onch$ll[ii] <- b[1] for some index ii.

Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48
0

I think you might be able to benefit from the question and answer given at: How to optimize the following code with nested while-loop? Multicore an option?

This question discusses many of the relevant principles and also indicates on how to optimise (vectorize) the code so in improves the execution.

Community
  • 1
  • 1
Jochem
  • 3,295
  • 4
  • 30
  • 55
0

I'll be making edits to this answer to explain it. In the meantime, give this a try. I don't have the FSA package (install.packages is squawking at me that it's not available for R 2.15.2), so I haven't tested it.

onch.removal <- lapply(1:nrow(onch),function(x) {
    pr1 <- removal(c(onch[x,"c1"],onch[x,"c2"],onch[x,"c3"]))
    return(list(est=summary(pr1)[[1]],ll=confint(pr1)[1],ul=confint(pr1)[2]))
})
onch <- data.frame(onch,onch.removal)
Blue Magister
  • 13,044
  • 5
  • 38
  • 56
0

There is a fast solution using data.table, no need for loops.

library(data.table)
library(FSA)

setDT(onch)

onch[, v_est := summary(removal(c(c1, c2, c3)))[1] , by= site]
onch[, v_ll := confint(removal(c(c1, c2, c3)))[1] , by= site]
onch[, v_ul := confint(removal(c(c1, c2, c3)))[1,2] , by= site]
rafa.pereira
  • 13,251
  • 6
  • 71
  • 109