当前位置:  开发笔记 > 编程语言 > 正文

R函数返回多个数据帧

如何解决《R函数返回多个数据帧》经验,为你挑选了1个好方法。

我具有以下功能来返回9个数据帧:

split_data <- function(dataset, train_perc = 0.6, cv_perc = 0.2, test_perc = 0.2)

{

m <- nrow(dataset)
n <- ncol(dataset)

#Sort the data randomly
data_perm <- dataset[sample(m),]

#Split data into training, CV, and test sets
train <- data_perm[1:round(train_perc*m),]
cv <- data_perm[(round(train_perc*m)+1):round((train_perc+cv_perc)*m),]
test <- data_perm[(round((train_perc+cv_perc)*m)+1):round((train_perc+cv_perc+test_perc)*m),]

#Split sets into X and Y
X_train <- train[c(1:(n-1))]
Y_train <- train[c(n)]

X_cv    <- cv[c(1:(n-1))]
Y_cv    <- cv[c(n)]

X_test  <- test[c(1:(n-1))]
Y_test <- test[c(n)]

}

我的代码运行良好,但是没有创建数据框。有没有办法做到这一点?谢谢



1> manotheshark..:

这会将九个存储data.frameslist

split_data <- function(dataset, train_perc = 0.6, cv_perc = 0.2, test_perc = 0.2) {

  m <- nrow(dataset)
  n <- ncol(dataset)

  #Sort the data randomly
  data_perm <- dataset[sample(m),]

  # list to store all data.frames
  out <- list()

  #Split data into training, CV, and test sets
  out$train <- data_perm[1:round(train_perc*m),]
  out$cv <- data_perm[(round(train_perc*m)+1):round((train_perc+cv_perc)*m),]
  out$test <- data_perm[(round((train_perc+cv_perc)*m)+1):round((train_perc+cv_perc+test_perc)*m),]

  #Split sets into X and Y
  out$X_train <- train[c(1:(n-1))]
  out$Y_train <- train[c(n)]

  out$X_cv <- cv[c(1:(n-1))]
  out$Y_cv <- cv[c(n)]

  out$X_test <- test[c(1:(n-1))]
  out$Y_test <- test[c(n)]

  return(out)

}

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有