[R] Faster way to combine data sets with different date ranges

Gabor Grothendieck ggrothendieck at gmail.com
Sat Nov 8 00:38:15 CET 2008


See ?merge.  Also ?merge.zoo in the zoo package and sqldf
in the sqldf package (http://sqldf.googlecode.com).

Please read the last line to every message to r-help.

On Fri, Nov 7, 2008 at 6:19 PM, t c <mudiver1200 at yahoo.com> wrote:
> I am trying to combine two data sets, one with daily values and one with weekly values.  SurveyData conatins environmental data collected on a daily basis.  sat.data contains satellite sea surface temperature that is an average of satellite measurements over a six day period.  I would like to combine the two files so that my output file has the daily dates from SurveyData and the weekly average from sat.data that corresponds to that day.  I have written a loop that does exactly what I want, but it takes a very long time.  Is there a faster way to do this?  What I have so far is below.
> Thanks,
> Tim
>
> #Compare dates
>  for(i in 1:length(SurveyData$Date))
>  {
>  print(i)
>  for(j in 1:length(sat.data$Date.Start))
>   {
>   if(
>   sat.data$Date.Start[j]<=SurveyData$Date[i] &
>   sat.data$Date.End[j]>=SurveyData$Date[i])
>   survey.data[i]<- sat.data$data[j]
>   }
>  }
>
> #Create data frame with final data
>  final.data<-data.frame(SurveyData$Record,SurveyData$Date,survey.data)
>
> #Write data to file
>  write.csv(final.data,file="Combined.csv")
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list