Wednesday, 2 October 2013

creating a sequence of columns in a dataframe based upon an index for loop or using plyr in r

creating a sequence of columns in a dataframe based upon an index for loop
or using plyr in r

I am new to R and this website: I have a complex problem in which I have
to create 24 hourly data frames in which each dataframe contains hourly
demand for a product as 1 column and the next 8 columns contain hourly
temperatures. If I am referring to the 8 am hour demand, the dataframe for
8am will contain a column for Demand then eight columns for temperature
ranging from the most current hour to the 7 past hours. The additional
complication is that for earlier hours i.e. "4AM", I have to get yestrdays
temperatures for some columns. I am hitting my head against the wall
trying to figureout how to do this in a vectorize langauge or use apply or
plyr to solve. demand8AM Temp8AM Temp7AM Temp6AM...Temp1AM
Demand4AM Temp4AM Temp3AM Temp2AM Temp1AM Temp12AM Temp11pm(Lag)
Temp10pm(Lag) In my code Hours are numbers 1 is 12AM etc...
Here is some simple code I created to create the dataset I am dealing
with. #Creating some Fake Data
require(plyr)
# setting up some fake data
set.seed(31)
foo <- function(myHour, myDate){
rlnorm(1, meanlog=0,sdlog=1)*(myHour) + (150*myDate)
}
Hour <- 1:24
Day <-1:90
dates <-seq(as.Date("2012-01-01"), as.Date("2012-3-30"), by = "day")
myData <- expand.grid( Day, Hour)
names(myData) <- c("Date","Hour")
myData$Temperature <- apply(myData, 1, function(x) foo(x[2], x[1]))
myData$Date <-dates
myData$Demand <-(rnorm(1,mean = 0, sd=1)+.75*myData$Temperature )
## ok, done with the fake data generation.
Thank you for any help

No comments:

Post a Comment