-1

I have a lot of dataframes with 8208 rows and would like to split each of them into 19 dataframes each with 432 rows. I assigned the following function:

splitter <- function(x) {
 a <- x[1:432,]
 b <- x[433:864,]
 c <- x[865:1296,]
 d <- x[1297:1728,]
 e<- x[1729:2160,]
 f <- x[2161:2592,]
 g <- x[2593:3024,]
 h <- x[3025:3456,]
 i <- x[3457:3888,]
 j <- x[3889:4320,]
 k <- x[4321:4752,]
 l <- x[4753:5184,]
 m <- x[5185:5616,]
 n <- x[5617:6048,]
 o <- x[6049:6480,]
 p <- x[6481:6912,]
 q <- x[6913:7344,]
 r <- x[7345:7776,]
 s <- x[7777:8208,]
assign(paste0('1',x), a, envir = globalenv())
assign(paste0('2',x), b, envir = globalenv())
assign(paste0('3',x), c, envir = globalenv())
assign(paste0('4',x), d, envir = globalenv())
assign(paste0('5',x), e, envir = globalenv())
assign(paste0('6',x), f, envir = globalenv())
assign(paste0('7',x), g, envir = globalenv())
assign(paste0('8',x), h, envir = globalenv())
assign(paste0('9',x), i, envir = globalenv())
assign(paste0('10',x), j, envir = globalenv())
assign(paste0('11',x), k, envir = globalenv())
assign(paste0('12',x), l, envir = globalenv())
assign(paste0('13',x), m, envir = globalenv())
assign(paste0('14',x), n, envir = globalenv())
assign(paste0('15',x), o, envir = globalenv())
assign(paste0('16',x), p, envir = globalenv())
assign(paste0('17',x), q, envir = globalenv())
assign(paste0('18',x), r, envir = globalenv())
assign(paste0('19',x), s, envir = globalenv())}

When I try the code on a dataframe, I get the error messages:

Error in assign(paste0("1", x), a, envir = globalenv()) : variable names are limited to 10000 bytes In addition: Warning message: In assign(paste0("1", x), a, envir = globalenv()) : only the first element is used as variable name

GNee
  • 117
  • 2
  • 9
  • 2
    You might be interested in a combination of `split` and `rep` which could produce a list of data.frames. It may be helpful to read gregor's answer to [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for tips on building and working with lists of data.frames. – lmo Jan 11 '17 at 18:58
  • x is a data.frame and "paste0(1,x) results in a data.frame hence the error. Do you mean to name the data X1,X2,... so on? – ab90hi Jan 11 '17 at 19:02

1 Answers1

1

Specifically in regards to your code for assigning a name to each dataframe object you are pasting a numeric and the entirety of 'x'. That is where you are getting the error for the number of bytes and the warning for elements. If you want to stick with your current code you could replace the paste0("1",x) with paste0("df1").

To be perfectly honest though because assign(x, value, pos = -1, envir = as.environment(pos),inherits = FALSE, immediate = TRUE) requires that x be passed as a character string you could simply just define it as :

assign("df1", a, envir = globalenv())

Your solution isn't the most elegant, and if you need to do a more 'random' split of the data I would suggest other methods, per lmo. Something like --

splitter<-function(x,groups=10){
  dd<-as.data.frame(x)
  dd$split<-sample(groups,size=nrow(dd),replace=T)
  for(i in 1:groups){
    ddd<-dd[dd$split==i,]
    assign(paste0("X",i),ddd,envir=globalenv())
  }
}
splitter(data.set,10)

You could get away with eliminating the loop but that is for you to decide. Just note the above code the dataframe sizes wouldn't be exactly equal but should be close to equal in rows...

Travis Gaddie
  • 152
  • 10