-1

I already posted a related question (Create a new vector by appending elements between them in R). I would like to know whether it's possible to increment a vector with a specified number of elements (like accumulate() from purr package).

In fact, I'm working with a vector of 16000 genes. I'm trying to write a for loop where at each iteration, 100 genes should be knocked out from the data set and proceed some clustering analysis (clustering with 16000 genes, clustering with 15900 genes, clustering with 15800 genes, etc.) My idea is to make a list from the vector where each element of it is a vector of genes incremented by 100 (first element 100 genes, second element 200 genes, third element 300 genes and the 160th element, the total 16000 genes). With accumulate (), I can increment one by one only between two following elements. Is there a way to make it increment 100 by 100?

Thank you all once again for your help!

SHADJI
  • 1
  • 1
  • Welcome to Stack Overflow! Can you please edit your question into a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to make it easier for others to help? – Mohamed Desouky Jun 22 '22 at 10:56

1 Answers1

0

Instead of a for loop, you could use a while loop and build a new list every time. It's not the most efficient way of doing this, but considering your dataset size, it should do the trick.

Here is some code to help you get started:

# Create a list of values
my_list <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23)

# Get the length of your list
max_len <- length(my_list)

# While [max_len] is positive, create a new list of [max_len] elements and decrement [max_len] by some value (here, 10) for the next list
while (max_len > 0) {
    new_list = my_list[1:max_len]
    print(new_list)
    max_len <- max_len - 10
}

Hope that helps !

Milo Parigi
  • 1,009
  • 7
  • 10