redvyper
redvyper

Reputation: 137

R: How to create dynamic subsets of data.table with a paired sequence of start, end row sequences

I.E. Splitting Data Table into dynamic quantity of variable chunks based off of row index

I am trying to split a data.table into chunks by row index.

The data is dynamic and the number of chunks may change & vary in size.

For example:

MyDT is a 2675 row, 98 column data table.

I've determined that for this dataset that I want rows 3-796, 802-857, 936-952, and 1206-2623 as their own separate chunks that I can call/save as a separate variable/etc.

I want the script to be dynamic and not require manual inputting of indexes. I can find any length and quantity of chunks currently, I just can't export them into a clean array/list. I.e. chunk 1 is DAT[1] , chunk 2 is DAT[2], and so on

[Background for the curious, I am importing data into R from an instrument that outputs its data from multiple experiments into a "messy" text file. I am separating the numeric, matrix form data from the rest of the cluttered output. Sometimes my data file has info from 2 experiments, 3, 4, etc...]

Upvotes: 0

Views: 200

Answers (1)

akrun
akrun

Reputation: 887213

If we want to store it in a list, create a sequence of 'start', 'end' vectors, use that to subset the data.table with Map

library(data.table)
start <- c(3, 802, 936, 1206)
end <- c(796, 857, 952, 2623)
lst_out <- Map(function(i, j) MyDT[i:j], start, end)

Upvotes: 1

Related Questions