简体   繁体   English

R使用for循环从一个大的csv文件子集并创建较小的csv文件

[英]R subsetting and creating smaller csv files from one large csv file using for loop

I have a large data set(csv) of temperatures which I have to subset by date (years) to create 5 smaller ones by decade. 我有一个大的温度数据集(csv),必须按日期(年)子集才能按十年创建5个较小的温度集。 So data from the years 1960-1969 will be one smaller csv file , data from years 1970-1979 will be another csv file etc. I've been trying to do this using a for loop 因此,从1960年至1969年的数据将是一个较小的csv文件,从1970年至1979年的数据将成为另一个csv文件,等等。我一直在尝试使用for循环进行此操作

so something like this : 所以像这样:

IDs<-unique(df$ID)
for (i in 1:length(IDs)){ 
  temp <- df[df$ID==IDs[i],]
  #more things to do with temp
}

But I'm not sure exactly how to go about this. 但是我不确定该怎么做。 The for loop should create the new csv files that have been subsetted by date from the existing large data set. for循环应创建新的csv文件,这些文件已按日期从现有大数据集中按日期进行了子集化。

This seems pretty generic, and thus searchable. 这似乎很通用,因此可以搜索。 I did a Google search, and this was literally the first thing I found. 我做了一个Google搜索,这实际上是我发现的第一件事。

http://www.r-bloggers.com/splitting-a-large-csv-file-into-separate-smaller-files-based-on-values-within-a-specific-column/ http://www.r-bloggers.com/splitting-a-large-csv-file-into-separate-smaller-files-based-on-values-within-a-specific-column/

I tried the script listed there and it worked perfect. 我尝试了那里列出的脚本,并且效果很好。 Give it a go, and see how you get along. 试一试,看看你如何相处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM