performance - Fill Matrix fast - R -


i started prediction models in r , have performance question:

currently have 2 datasets sunset , sunrise data.

i computed them years 2011 , 2012 (daily data). real data contains first half of each month. tried integrate sundata in data frame loop:

library(lubridate) library(maptools) 

sunrise functioin from: https://gist.github.com/hilaryparker/2a83ca521353e8478c92

sunrise.set <- function (lat, long, date, timezone = "utc", num.days = 1)   {   lat.long <- matrix(c(long, lat), nrow = 1)   day <- as.posixct(date, tz = timezone)   sequence <- seq(from = day, length.out = num.days, = "days")   sunrise <- sunriset(lat.long, sequence, direction = "sunrise",                        posixct =true)   sunset <- sunriset(lat.long, sequence, direction = "sunset",                        posixct = true)   ss <- data.frame(sunrise, sunset)   ss <- ss[, -c(1, 3)]   colnames(ss) <- c("sunrise", "sunset")   return(ss) }   datetime<-c(seq(from = as.posixct("2011-01-01 00:00"), = as.posixct("2011-01-19 24:00"), = "hour"),seq(from =as.posixct("2011-02-01 00:00"), = as.posixct("2011-02-19 24:00"), = "hour")) train<-data.frame(datetime,1)  date<-force_tz(as.posixlt(seq(as.date("2011/01/01"),as.date("2011/02/28"), = "day"), format="%y/%m/%d",tzone="america/detroit"),tzone="america/detroit") sunrise<-data.frame(date,sunrise.set(38.889931,-77.009003,"2011/01/01", timezone = "america/detroit", num.days = 59))   sunrise.train<-as.posixlt(na) sunset.train<-as.posixlt(na) (i in 1:length(train$datetime)){  for( j in 1:length(sunrise$date)){print(i);print(j)     if(as.date(as.posixlt(train$datetime[i]))==sunrise$date[j])  {sunrise.train[i]<-sunrise$sunrise[j];sunset.train[i]<-sunrise$sunset[j]} }}  train.modified<-data.frame(train,sunrise.train,sunset.train) 

unfortunately loop small, takes 2-3 hours compute loop full data. there faster way "fill" training data frame suitible data?

thank much!

edit:changed code appearance

you want simple merge

update:

# reduce data testing train <- train[1:100,] sunrise <- sunrise[1:10,]  yf <- function() { (i in 1:length(train$datetime)) {   (j in 1:length(sunrise$date)) {     if (as.date(as.posixlt(train$datetime[i])) == sunrise$date[j])  {       sunrise.train[i] <- sunrise$sunrise[j]       sunset.train[i] <- sunrise$sunset[j]     }   } } list(sunrise.train, sunset.train)   }  system.time(r <- yf()) # ~ 3 sek 100 x 10  train.modified <- data.frame(train, sunrise = r[[1]], sunset = r[[2]]) # results 

using data.table:

require(data.table) setdt(train) setdt(sunrise)  # reformat dates equal formats train[, date := as.date(as.posixlt(datetime))] sunrise[, date := as.date(date)]  myrez <- merge(train, sunrise, = "date", all.x = t) myrez <- myrez[, -1]  setdt(train.modified)  head(train.modified) #              datetime x1             sunrise              sunset #1: 2011-01-01 00:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #2: 2011-01-01 01:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #3: 2011-01-01 02:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #4: 2011-01-01 03:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #5: 2011-01-01 04:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #6: 2011-01-01 05:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 head(myrez) #              datetime x1             sunrise              sunset #1: 2011-01-01 00:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #2: 2011-01-01 01:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #3: 2011-01-01 02:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #4: 2011-01-01 03:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #5: 2011-01-01 04:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 #6: 2011-01-01 05:00:00  1 2011-01-01 07:26:39 2011-01-01 16:56:37 

check equality:

all.equal(train.modified, myrez) # [1] "column 'sunrise': attributes: < component “tzone”: 1 string mismatch > mean relative difference: 1.947453e-05" 

there minor difference in results, because of time zone miss-specification. should define needed time zone when converting dated.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -