也许这对你有用:
# A simple function (EDIT: FIXED)
Split_and_save_DF <- function(DF, split){
# Spliting your data frame by columns to get several data.frames
DFlist <-lapply(seq(1, ncol(DF), split), function(x, i){x[, i:(i+(split-1))]}, x=DF)
# Saving each data.frames as .txt file
invisible(sapply(1:length(DFlist), function(x, i) write.table(x[[i]], file=paste0('DF', i, '.txt')), x=DFlist))
}
Example
DF <- data.frame(matrix(rnorm(144*12, 100, 30), ncol=144))
dim(DF) # a dataframe with 12 rows and 144 cols
Split_and_save_DF(DF=DF, split=3) # will produce 48 DF's
Where DF
是数据框,并且split
是您希望数据框分割的列数。
这不是一个很好的答案,但它可以满足您的要求。
此函数将拆分您的 DF,并将每个新的 DF 保存在当前工作目录中,名称如下:DF1.txt
, DF2.txt
, DF3.txt
....这样您就可以通过执行以下操作来读取每个文件:
read.table("DF1.txt", header=TRUE) # and so on
为了检查输出:
dim(read.table("DF1.txt", header=TRUE)) # checking dims of new DF's
[1] 12 3