An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120906/82a765d5/attachment.pl>
use of ddply() within function
3 messages · Jeff Newmiller, Franckx Laurent
As the footer says, "provide commented, _minimal_, self-contained, reproducible code."
This is hardly "minimal" or reproducible.
Anyway, this probably ought to qualify as a plyr FAQ of some sort: don't use the summarise function as the FUN argument within a function. Define a named or anonymous function of your own as the help page for ddply describes. Too many layers of syntactic sugar are involved for summarise to work within a function.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Franckx Laurent <laurent.franckx at vito.be> wrote:
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 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.
Problem solved, thanks.
I've used this instead: COSTaux_AVCAR <- ddply(CARaux,c("SC","YEAR"),.fun = function(xx){sum(xx[ ,costcarin])})
-----Original Message-----
From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.CA.us]
Sent: donderdag 6 september 2012 14:56
To: Franckx Laurent; 'r-help at r-project.org'
Subject: Re: [R] use of ddply() within function
As the footer says, "provide commented, _minimal_, self-contained, reproducible code."
This is hardly "minimal" or reproducible.
Anyway, this probably ought to qualify as a plyr FAQ of some sort: don't use the summarise function as the FUN argument within a function. Define a named or anonymous function of your own as the help page for ddply describes. Too many layers of syntactic sugar are involved for summarise to work within a function.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Franckx Laurent <laurent.franckx at vito.be> wrote:
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 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.
________________________________ VITO Disclaimer: http://www.vito.be/e-maildisclaimer