Reputation: 123
I looked at several posts in order to get help to solve my issue, but no luck implementing a solution. My problem is simply, I want to pass a variable name to the cast function from the Reshape package. The variable "Time" is an argument in a user define function I am creating. I want to pass this argument through the cast function.
Pseudo Data I always use:
dates <-seq(as.Date("2010-01-01"), by = "day", length.out= 1080)
myData <- expand.grid( Day, Hour)
names(myData) <- c("Date","Hour")
myData$Adspend <- apply(myData, 1, function(x) foo(x[2], x[1]))
myData$Date <-dates
myData$Demand <-(rnorm(1,mean = 0, sd=1)+.75*myData$Adspend)
myData$Hour<-as.factor(myData$Hour)
Here is my partial user defined function I am testing:
AddLag <- function(DF,Date,Time,RESP,AD,LAG=9) {
Lags<-24-LAG
ADDATE<-DF[,c(1,2,4)]
RESPDATE<-cbind(DF[[Date]],DF[[Time]],DF[[RESP]])
HOURAD<-melt(ADDATE, id=c(Date,Time), measured =c(AD))
HOURAD<- cast(HOURAD,as.formula(paste0(" ...~"),Time))
return(HOURAD)
}
The errors I am getting. I've tried various configurations of paste I suspect this is a simple problem that I can't figure out:
Bo<-AddLag(myData,"Date","Hour","Demand","Adspend")
Error in parse(text = x) : <text>:2:0: unexpected end of input
1: ...~
^
Upvotes: 2
Views: 882
Reputation: 123
I figured it out. It was a misunderstanding of how paste0 works with the as.formula function. Here is how the cast function should have been written:
require(reshape2)
HOURAD<- dcast(HOURAD,as.formula(paste0("... ~",Time))) ## was a parenthesis error here
Upvotes: 2