How can I select n number of subsets from a data frame by taking every nth row for subset 1 then nth +1 row for subset 2 then nth+3 for subset3 until nth=n
I have used
subset<-data[seq(nth,length,n),]
But this gives one subset then I have to keep changing nth from 1...n to get different subsets.e.g using a small data(106 rows x 742 columns) set to get 10 subsets of every 10th row
subset1<-data[seq(1,106,10),]
subset2<-data[seq(2,106,10),]
subset3<-data[seq(3,106,10),]
Is there any way to do this better?
From going through the FAQ I have tried using loops like
sub<-function(data,nth,length,n){
sub<-data[seq(nth,length,n),]
for(n in 1:(sub)){
sub2<-sub[nth,]+1,sub3<-sub[nth,]+2,sub4<-sub[nth,]+3) }
su<-(sub,sub2, sub3,sub4)
return(su)
}
sub(data=gag11p,n=1,length=106,10)
This returns 3 data list with only the last variable in the data frame,I am not sure where I went wrong, also how can I just get the name of the subset instead of a data frame as I want to apply a PLS calibration function to the subsets created
Please forgive and correct any mistakes since I am now learning programing and R.
A one liner using lapply
borrowing the function idea from @Chase.
foo2 = function(data, nSubsets, nSkip){
lapply(1:nSubsets, function(n) data[seq(n, NROW(data), by = nSkip),])
}
foo2(mtcars, 5, 15)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With