nouse
nouse

Reputation: 3461

Stepwise reducing the input dataframe in a loop

I need to do iteratively evaluate the variance of a dataset while i reduce the data.frame row by row in each step. As an example

data <- matrix(runif(100),10,10)
perc <- list("vector")
sums <- sum(data)

for (i in 1:nrow(data)) {      
  data <- data[-1,]
  perc[[i]] <- sum(data)/sums # in reality, here are ~8 additonal lines of code
}

I dont like that data is re-initialized in every step, and that the loop breaks with an error, when data is emptied. So the questions are: 1. How to express data <- data[-1,] in an incrementing way (something like tmp <- data[-c(1:i),], which doesnt work? 2. Is there a way to stop the loop, before the last row is removed from data?

Upvotes: 0

Views: 46

Answers (3)

Kunal Puri
Kunal Puri

Reputation: 3427

You can write the loop part like this:

for (i in 2:nrow(data)) {      
  perc[[i - 1]] <- sum(data[i:nrow(data),])/sums # in reality, here are ~8 additonal lines of code
}

Upvotes: 1

RHertel
RHertel

Reputation: 23798

You could try

set.seed(123)
data <- matrix(runif(100),10,10)
sums <- sum(data)
perc <- lapply(2:nrow(data),function(x) sum(data[x:nrow(data),]/sums))

The above code yields the same result as your original code, but without error message and without modifying data.

perc1 <- list()
for (i in 1:nrow(data)) {      
  data <- data[-1,]
  perc1[[i]] <- sum(data)/sums 
}
identical(perc,perc1)
#[1] TRUE

If you wish to preserve the for loop in order to perform other calculations within the loop, you could try:

for (i in 2:nrow(data)) {
  perc[[i-1]] <- sum(data[i:nrow(data),])/sums
  # do more stuff here
}
identical(perc,perc1)
#[1] TRUE

If you are using the loop index i for other calculations within the loop, you will most probably need to replace it with i-1. It may depend on what is calculated.

Upvotes: 1

Richard Telford
Richard Telford

Reputation: 9923

You can use lapply

res <- lapply(2:nrow(data), function(i)sum(data[i:nrow(data),])/sums)

Upvotes: 1

Related Questions