我有一个包含10万行的txt文件(“JT.txt”)。我想以最简单的方式将它分成同一目录中的每个200.000行的几个文件,并使用模式“JT_1.txt”,“JT_2.txt”等调用文件名。 我怎么能用R?
做到这一点答案 0 :(得分:1)
在这里分享我的逻辑:
mtcars$rows <- 1:nrow(mtcars) # create a index
mtcars$rows <- cumsum(mtcars$rows %% 2) # this creates blocks of 2 rows
# now we just split it : I think , this should work. add write.table() inside lapply()
lapply(split(mtcars, mtcars$rows), function(x) paste0("mytext", unique(x[["rows"]]), ".csv"))
答案 1 :(得分:0)
#create a sequence of indices for splitting the data by increment of N
N = 6;
splitIndices = seq(1,nrow(mtcars),N)
#for each index, split dataset and export
lapply(splitIndices,function(x) {
minIndex = x;
maxIndex = min(x+N-1,nrow(mtcars));
DF = mtcars[minIndex:maxIndex,]
write.table(DF,paste0("JT_",1+as.integer(x/N),"_.txt"))
} )