Vishesh Tayal
Vishesh Tayal

Reputation: 25

Split the dataframe into subset dataframes and naming them on-the-fly (for loop)

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!

Upvotes: 1

Views: 1186

Answers (2)

Ryogi
Ryogi

Reputation: 5617

A small variation on this solution

ls <- split(final_9880, rep(0:9, each = 1000, length.out = 9880))  # edited to Roman's suggestion
for(i in 1:10) assign(paste("test",i,sep="_"), ls[[i]])

Your command for binding should work.

Edit

If you have many dataframes you can use a parse-eval combo. I use the package gsubfn for readability.

library(gsubfn)
nms <- paste("test", 1:10, sep="_", collapse=",")
eval(fn$parse(text='do.call(rbind, list($nms))'))

How does this work? First I create a string containing the comma-separated list of the dataframes

> paste("test", 1:10, sep="_", collapse=",")
[1] "test_1,test_2,test_3,test_4,test_5,test_6,test_7,test_8,test_9,test_10"

Then I use this string to construct the list

list(test_1,test_2,test_3,test_4,test_5,test_6,test_7,test_8,test_9,test_10)

using parse and eval with string interpolation.

eval(fn$parse(text='list($nms)'))

String interpolation is implemented via the fn$ prefix of parse, its effect is to intercept and substitute $nms with the string contained in the variable nms. Parsing and evaluating the string "list($mns)" creates the list needed. In the solution the rbind is 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.

do.call("rbind", sapply(ls(pattern = "test_"), get, simplify = FALSE))

ls finds all variables with a pattern "test_"

sapply retrieves all those variables and stores them in a list

do.call flattens the list row-wise.

Upvotes: 2

mnel
mnel

Reputation: 115382

No for loop required -- use split

data <- data.frame(a = 1:9880, b = sample(letters, 9880, replace = TRUE))

splitter <- (data$a-1) %/% 1000

.list <- split(data, splitter)

lapply(0:9, function(i){
  assign(paste('test',i,sep='_'), .list[[(i+1)]], envir = .GlobalEnv)
  return(invisible())
})

all_9880<-rbind(test_0,test_1,test_2,test_3,test_4,test_5,test_6,test_7,test_8,test_9)

identical(all_9880,data)
## [1] TRUE

Upvotes: 2

Related Questions