I have 9880 records in a data frame, I am trying to split it into 9 groups of 1000 each and the last group will have 880 records and also name them accordingly. I used for-loop for 1-9 groups but manually for the last 880 records, but i am sure there are better ways to achieve this,
library(sqldf)
for (i in 0:8)
{
assign(paste("test",i,sep="_"),as.data.frame(final_9880[((1000*i)+1):(1000*(i+1)), (1:53)]))
}
test_9<- num_final_9880[9001:9880,1:53]
also am unable to append all the parts in one for-loop!
#append all parts
all_9880<-rbind(test_0,test_1,test_2,test_3,test_4,test_5,test_6,test_7,test_8,test_9)
Any help is appreciated, thanks!
A small variation on this solution
Your command for binding should work.
Edit
If you have many dataframes you can use a parse-eval combo. I use the package
gsubfnfor readability.How does this work? First I create a string containing the comma-separated list of the dataframes
Then I use this string to construct the list
using
parseandevalwith string interpolation.String interpolation is implemented via the
fn$prefix ofparse, its effect is to intercept and substitute$nmswith the string contained in the variablenms. Parsing and evaluating the string"list($mns)"creates the list needed. In the solution therbindis included in the parse-eval combo.EDIT 2
You can collect all variables with a certain pattern, put them in a list and bind them by rows.
lsfinds all variables with a pattern “test_”sapplyretrieves all those variables and stores them in a listdo.callflattens the list row-wise.