gpt4 book ai didi

R函数返回多个数据帧

转载 作者:行者123 更新时间:2023-12-01 10:31:40 24 4
gpt4 key购买 nike

我有以下函数来返回 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)]

}

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

最佳答案

这将存储九个 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)

}

关于R函数返回多个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41855555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com