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.
I suggest you store all of these different subsets into a single
listobject. I’m not sure I 100% followed your code above, but I think this does what you want:What’s happening?
Here’s an example using the
mtcarsdata. Note the dataset only has 32 rows, so the function automatically handles subscripts that are out of bounds and doesn’t throw a warning / error:FOO(mtcars, 5, 15)