0

I would like to get some help to merge my list of data.frame's to one big data.frame.

I use a function to split the my dataset into k-folds, then I want to get all of these subsets combined to one big data.frame

I use this function to split the data into different sets and then put them in the list:

#Function for splitting the data in to k-folds
split=function(data, k=5, random=TRUE){

  #Check if the data should be randomized
  if(random==TRUE){
    #randomize the data
    data=data[sample(nrow(data)),]
  }

  #Initial variables
  returnList=list()
  n=nrow(data)
  folds=cut(1:n, breaks = k, labels = FALSE)
  uniqueValues=unique(folds)

  for (i in uniqueValues) {
    dataIndex=which(folds==i, arr.ind = FALSE)
    returnList[[i]]=data[dataIndex,]
  }

  return(returnList)

}

For simplicity I will use this on the swiss data set which is available in base r.

data=swiss
splitData=split(data = data)

I now, want to iterate over the list and make a big data.frame that contain all the subsets but 1. This is the part that I need help with.

for (i in 1:length(splitData)) {
  trainData=as.data.frame(splitData[-i])
  testData=as.data.frame(splitData[i])
}
Jesper.Lindberg
  • 313
  • 1
  • 5
  • 14

0 Answers0