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]]
Gabor Grothendieck
2008-Nov-07 23:38 UTC
[R] Faster way to combine data sets with different date ranges
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. > >
John Kane
2008-Nov-08 16:22 UTC
[R] Faster way to combine data sets with different date ranges
Just expand the weekly data set to have an entry for each day and cbind
SurveyData with the new Modified.sat.data.
I was having a similar problem but with dates and here was a solution suggested
by Gustaf Rydevik using rep() with the each option.
=======================================================================time<-rep(1:24,
365)
dates<-seq(as.Date("01012005",format="%d%m%Y"),as.Date("31122005",format="%d%m%Y"),by=1)
TimeFrame<-data.frame(time)
TimeFrame$dates<-rep(dates,each=24)
========================================================================
--- On Fri, 11/7/08, t c <mudiver1200 at yahoo.com> wrote:
> From: t c <mudiver1200 at yahoo.com>
> Subject: [R] Faster way to combine data sets with different date ranges
> To: r-help at r-project.org
> Received: Friday, November 7, 2008, 6:19 PM
> 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.
__________________________________________________________________
Ask a question on any topic and get answers from real people. Go to Yahoo!
Answers and share what you know at http://ca.answers.yahoo.com