0

I have a for loop which prints a result, just a single number and an i(for 1:10)

(count <- (length(which(colSums(data2[, -8])==0))))
print(i)

When I run all parts of the loop manually it prints the correct count(decreasing in value). And I could manually put all the values in excel to plot. However, Id like in my loop for count and i to be added to a new data frame, so I can run this a thousand times to get a reliable result.

And get a dataframe like this:

i   count
1   10
2   9
3   8
4   6
5   4
6   4
7   4
8   3
9   2
10  0

So my question basically is: how do I extract 'count' and 'i' and create a dataframe to fit this in(make a row for each i).

Datasets and the rest of the loop is irrelevant for the solution I think.

Any help would be appreciated, cheers Jasper

EDIT

My current loop

for(i in 1:10) {

data2 <- data2[-sample(nrow(data2),size=1,replace=FALSE),]
(count <- (length(which(colSums(data2[, -8])==0))))
print(i)

}
4
  • 1
    count is a single number, independent of i in the code you've written...? Commented May 18, 2015 at 14:34
  • Count is indeed a single number, independent of i. > (count <- (length(which(colSums(data2[, -8])==0)))) [1] 9 Commented May 18, 2015 at 14:36
  • i is like time, it samples 10 times random rows and calculates the amount of columns with 0. There are 10 different samples, so its always dead after 10 tries. Commented May 18, 2015 at 14:38
  • 1
    I think you'll need to actually write your loop for this to make sense. I think you have some data and within the loop create data2 <- data[sample(1:nrow(data),10),], but I can only guess. Commented May 18, 2015 at 14:48

1 Answer 1

1

A more efficient approach would be to compute the column sums first and difference for each sample:

cs <- colSums(data2[,-8])

nsim   <- 10
res    <- vector("integer",nsim)
for (i in 1:nsim) res[i] = sum(!(cs - data2[sample(1:nrow(data2),1),-8]))

simres <- data.frame(i=1:nsim,res=res)

Note that length(which(x)) is the same as sum(x) here; and x==0 is !x.


Comments. It might also be a good idea to store your data in a matrix if it is all numeric and operations like this are frequent.

There are many alternatives to for loops in R. Here's an example typical for simulations:

res <- replicate(nsim,sum(!(cs - data2[sample(1:nrow(data2),1),-8])))

If you're taking many samples, you could also precompute the row-left-out value for every row:

norowres <- rowSums(!(cs-data2[,-8])) # not tested
res      <- sample(norowres,nsim,replace=TRUE)
Sign up to request clarification or add additional context in comments.

3 Comments

Don't subset a data.frame in a for loop. It's more efficient to fill a vector and put it into a data.frame after the loop.
@Roland Good to know; I'll switch to that.
Thanks, simres <- data.frame(i=1:nsim) was what I was looking for.

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.