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]
 }
7
  • 2
    how does onch looks like (e.g., names(onch)) ?? Commented Dec 26, 2012 at 20:04
  • 1
    Can you share a sample of the data? For example using the principles explained at: meta.stackexchange.com/questions/155375/… Commented Dec 26, 2012 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? Commented Dec 26, 2012 at 20:10
  • @Jochem For samples/examples, I prefer this question. Commented Dec 26, 2012 at 20:12
  • What is the function removal? Commented Dec 26, 2012 at 20:16

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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)

Comments

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.