Dear all, I am encountering problems with the application of ddply within the body of a self-defined function.
The script is the following: moncostcarmoto <- function(costtype){ costaux_result <- data.frame() for (purp in PURPcount){for (per in PERcount){ costcarin = paste(c("CS_",costtype,"CAR"),collapse="") costmotoin = paste(c("CS_",costtype,"MOTO"),collapse="") browser() COSTaux_AVCAR <- ddply(CARaux,c("SC","YEAR"),summarize,costcsolo = sum(CARaux[[costcarin]])) COSTaux_AVMOTO <- ddply(CARaux,c("SC","YEAR"),summarize,costmoto= sum(CARaux[[costmotoin]] )) COSTaux <- join(COSTaux_AVCAR,COSTaux_AVMOTO,by=c("SC","YEAR")) COSTaux[ ,"PURP"] <- as.factor(purp) COSTaux[ ,"PER"] <- as.factor(per) costaux_result <- rbind(costaux_result,COSTaux) }} return(costaux_result) } Summarizing what it is supposed to do: * In previous step of our analysis, we have constructed a dataframe, CARaux. CARaux represents different cost categories (fuel prices, taxes, maintenance) etc for cars and motorcycles. The costs are differentiated according to the projection year ("YEAR") considered, the policy scenario considered ("SC") and the car type considered. * The function moncostcarmoto takes as only argument one of four possible aggregate cost types: "MONCOST_EXCLTAX", "TAX",""FUELCOST" and "MONCOST_INCLTAX"". * The output of the function is a summary of the costs per YEAR and SCENARIO, represented for all possible trip purposes (enumerated in PURPcount) and period of the day (enumerated in PERcount). Note that these costs do not depend on PURP and PER in the base scenario, but this is something I may want to modify in the future (and which explains why I iterate over these categories). The function fails in the following line: COSTaux_AVCAR <- ddply(CARaux,c("SC","YEAR"),summarize,costcsolo = sum(CARaux[[costcarin]])) With the following error message: Error in (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, : object 'costcarin' not found When I debugged the function, I found the following: Browse[1]> costcarin [1] "CS_MONCOST_EXCLTAXCAR" Browse[1]> summary(CARaux[[costcarin]]) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0000000 0.0000000 0.0001605 0.0166700 0.0048570 0.1633000 Browse[1]> COSTaux_AVCAR <- ddply(CARaux,c("SC","YEAR"),summarize,costcsolo = sum(CARaux[["CS_MONCOST_EXCLTAXCAR"]])) Browse[1]> summary(COSTaux_AVCAR) SC YEAR costcsolo BAU:31 Min. :2000 Min. :6.202 1st Qu.:2008 1st Qu.:6.202 Median :2015 Median :6.202 Mean :2015 Mean :6.202 3rd Qu.:2022 3rd Qu.:6.202 Max. :2030 Max. :6.202 Browse[1]> COSTaux_AVCAR <- ddply(CARaux,c("SC","YEAR"),summarize,costcsolo = sum(CARaux[[costcarin]])) Error in (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, : object 'costcarin' not found Thus, providing CARaux[["CS_MONCOST_EXCLTAXCAR"]] as an argument to ddply() works, but CARaux[[costcarin]] does not, although costcarin = "CS_MONCOST_EXCLTAXCAR". Best regards Laurent ________________________________ VITO Disclaimer: http://www.vito.be/e-maildisclaimer [[alternative HTML version deleted]] ______________________________________________ R-help@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.