0
votes

I need every 7 elements in a row to be for the first column sum; for the second column sum, for the third column min, for the fourth column max and reaped the process from the 3rd to the 48th column of the data frame, named here dataset, which has in fact 50 columns and 84 rows. I'm expected to get a data frame with 12 rows and 48 columns in which each row element is a statistics (in following repeated order by column: sum, sum, min max) of each 7 elements in each of the 84 rows. I think maybe the problem could be in the for loop for the rows i, could be?

k=data.frame(matrix(nrow = 12,ncol = 48));
  for (i in 1:12) {
     for (j in 1:12) {
         k[i,4*j-3]=apply(dataset[ i:7,4*j-1], 2,sum)
         k[i,4*j-2]=apply(dataset[ i:(i+7),4*j], 2,sum)
         k[i,4*j-1]=apply(dataset[ i:(i+7),4*j+1], 2,min)
         k[i,4*j]=apply(dataset[ i:(i+7),4*j+2], 2,max)
      }
  };
1
Are you dong a rolling sum, min, max etc. The description is not very clear. If you can show a small example of dataset and expected output, would be goodakrun
How is this different from the question you posted earlier today?camille

1 Answers

0
votes

something like this?

stats <- NULL
for (i in 1:ncol(data)) {
    if (any(seq(1, ncol(data), by = 7) == i))) {
        stats[i] <- sum(data[,i])
    } else {
        if (any(seq(2, ncol(data), by = 7) == i))) {
            stats[i] <- sum(data[,i])
        } else {
            if (any(seq(3, ncol(data), by = 7) == i))) {
                stats[i] <- min(data[,i])
            } else {
            stats[i] <- max(data[,i])
            }
        }

    }
}