R 如何将CSV文件分割成小块?
我试图找出如何将CSV文件分割成小块。我想按任意数字或行分割。可能是20,1000,或者别的什么R 如何将CSV文件分割成小块?,r,R,我试图找出如何将CSV文件分割成小块。我想按任意数字或行分割。可能是20,1000,或者别的什么 setwd("C:/Users/my_path/test_folder/") mydata = read.csv("NHLData.csv") split(mydata, ceiling(seq_along(mydata)/20)) 错误:警告消息:在split.default(x=seq_len(nrow(x)),f=f,drop=drop,…):数据长度不是split变量的倍数 我也
setwd("C:/Users/my_path/test_folder/")
mydata = read.csv("NHLData.csv")
split(mydata, ceiling(seq_along(mydata)/20))
错误:警告消息:在split.default(x=seq_len(nrow(x)),f=f,drop=drop,…):数据长度不是split变量的倍数
我也试过这个
split(mydata, ceiling(seq_along(mydata)/(length(mydata)/20)))
相同错误:警告消息:在split.default(x=seq_len(nrow(x)),f=f,drop=drop,…):数据长度不是split变量的倍数
我在谷歌上搜索那些想法。我真的没有找到其他有用的东西。这一定很简单,对吧。使用“sample”函数,这会有所帮助
setwd("C:/Users/my_path/test_folder/")
mydata = read.csv("NHLData.csv")
# If you want 5 different chunks with same number of lines, lets say 30.
Chunks = split(mydata,sample(rep(1:5,30))) ## 5 Chunks of 30 lines each
# If you want 20 samples, put any range of 20 values within the range of number of rows
First_chunk <- sample(mydata[1:20,]) ## this would contain first 20 rows
# Or you can print any number of rows within the range
Second_chunk <- sample(mydata[100:70,] ## this would contain last 30 rows in reverse order if your data had 100 rows.
# If you want to write these chunks out in a csv file:
write.csv(First_chunk,file="First_chunk.csv",quote=F,row.names=F,col.names=T)
write.csv(Second_chunk,file="Second_chunk.csv",quote=F,row.names=F,col.names=T)
setwd(“C:/Users/my\u path/test\u folder/”)
mydata=read.csv(“NHLData.csv”)
#如果您想要5个具有相同行数的不同块,那么假设为30。
Chunks=split(mydata,sample(rep(1:5,30))##5个块,每个块30行
#如果需要20个样本,请将20个值的任意范围置于行数范围内
首先,chunk有一些解决方案,将read.csv
中的skip
和nrows
组合在一起,将为您提供所有需要读取的csv文件行…Ryguy72(72),不要创建多个帐户。为什么他什么都不接受?把他的问题记为正确吗?这可以接受吗?这能回答你的问题吗?