Jennifer Sabatier
2012-Aug-07 20:53 UTC
[R] Error using ddply inside user-defined function
Hi All, I *think* it's ddply because the function recognizes vr1, etc, in other parts of the function. Here's some code: # create dataset PROV.PM.FBCTS <- c(0.00 ,0.00, 33205.19, 25994.56, 23351.37, 26959.56 ,27632.58, 26076.24, 0.00, 0.00 , 6741.42, 18665.09 ,18129.59 ,21468.39 ,21294.60 ,22764.82, 26076.73) FBCTS.INV.TOT <- c(0 , 0, 958612, 487990, 413344, 573347, 870307, 552681 , 0, 0 , 163831 , 400161 , 353000, 358322 , 489969, 147379, 1022769) FBCTS.REC.TOT <- c(0 , 0, 1638818 ,297119 , 299436 ,414164 , 515735, 529001 , 0, 0 , 427341 , 625893 ,437854 , 407091, 425119 , 8663, 0) MECH.NAME <- c("Mechanism A","Mechanism A","Mechanism A","Mechanism A","Mechanism A","Mechanism A","Mechanism A","Mechanism A", "Mechanism B","Mechanism B","Mechanism B","Mechanism B","Mechanism B","Mechanism B","Mechanism B","Mechanism B","Mechanism B",) vn <- data.frame(MECH.NAME, PROV.PM.FBCTS, FBCTS.INV.TOT, FBCTS.REC.TOT) # create function allocation <- function(vr1, vr2, vr3) { d <- ddply(vn, "MECH.NAME", summarise, SUM = vr1 + vr2 + vr3) vn <- merge(vn, d, by.x="MECH.NAME", by.y="MECH.NAME", all=T) new <- (vr1+vr2+vr3) / vn$SUM return(new) } # run function vn$PROV.PM.FBCTS <- allocation(PROV.PM.FBCTS, FBCTS.INV.TOT, FBCTS.REC.TOT) Here's the error:> vn$PROV.PM.FBCTS <- allocation(PROV.PM.FBCTS, FBCTS.INV.TOT,FBCTS.REC.TOT) Error in eval(expr, envir, enclos) : object 'vr1' not found # If I pull the calculations out of the function it works: d <- ddply(vn, "MECH.NAME", summarise, SUM = sum(PROV.PM.FBCTS, FBCTS.INV.TOT, FBCTS.REC.TOT)) vn <- merge(vn, d, by.x="MECH.NAME", by.y="MECH.NAME", all=T) vn$PROV.PM.FBCTS <- (PROV.PM.FBCTS + FBCTS.INV.TOT + FBCTS.REC.TOT) / vn$SUM> vnMECH.NAME PROV.PM.FBCTS FBCTS.INV.TOT FBCTS.REC.TOT SUM 1 Mechanism A 0.00000000 0 0 7713774 2 Mechanism A 0.00000000 0 0 7713774 3 Mechanism A 0.34103091 958612 1638818 7713774 4 Mechanism A 0.10515004 487990 297119 7713774 5 Mechanism A 0.09543077 413344 299436 7713774 6 Mechanism A 0.13151418 573347 414164 7713774 7 Mechanism A 0.18326628 870307 515735 7713774 8 Mechanism A 0.14360783 552681 529001 7713774 9 Mechanism B 0.00000000 0 0 5402533 10 Mechanism B 0.00000000 0 0 5402533 11 Mechanism B 0.11067280 163831 427341 5402533 12 Mechanism B 0.19337580 400161 625893 5402533 13 Mechanism B 0.14974155 353000 437854 5402533 14 Mechanism B 0.14565046 358322 407091 5402533 15 Mechanism B 0.17332290 489969 425119 5402533 16 Mechanism B 0.03309685 147379 8663 5402533 17 Mechanism B 0.19413964 1022769 0 5402533 Can anyone help me figure out why I can't put this calculation in a function? Is this a systemic thing I should look for in the future? I know there are other ways to do what I want, but I am not very curious about this. Best, Jen [[alternative HTML version deleted]]
A few comments in-line below On 8/7/2012 1:53 PM, Jennifer Sabatier wrote:> PROV.PM.FBCTS <- c(0.00 ,0.00, 33205.19, 25994.56, 23351.37, 26959.56 > ,27632.58, 26076.24, 0.00, 0.00 , 6741.42, 18665.09 ,18129.59 ,21468.39 > ,21294.60 ,22764.82, 26076.73) > FBCTS.INV.TOT <- c(0 , 0, 958612, 487990, 413344, 573347, 870307, > 552681 , 0, 0 , 163831 , 400161 , 353000, 358322 , 489969, 147379, > 1022769) > FBCTS.REC.TOT <- c(0 , 0, 1638818 ,297119 , 299436 ,414164 , 515735, > 529001 , 0, 0 , 427341 , 625893 ,437854 , 407091, 425119 , 8663, > 0) > MECH.NAME <- c("Mechanism A","Mechanism A","Mechanism A","Mechanism > A","Mechanism A","Mechanism A","Mechanism A","Mechanism A", "Mechanism > B","Mechanism B","Mechanism B","Mechanism B","Mechanism B","Mechanism > B","Mechanism B","Mechanism B","Mechanism B",)Line breaks have messed up this data and there is an extra comma at the end of MECH.NAME> vn <- data.frame(MECH.NAME, PROV.PM.FBCTS, FBCTS.INV.TOT, FBCTS.REC.TOT)It is easier to just give the final object. See the output of dump("vn", file="")> # create function > > allocation <- function(vr1, vr2, vr3) > { > > d <- ddply(vn, "MECH.NAME", summarise, SUM = vr1 + vr2 + vr3)ddply is looking for a column named vr1 (etc.), not a column whose name is that of the character value of the variable vr1. There is not a straightforward way to do this with summarise, but you can use something like function(DF) { data.frame(SUM = DF[[vr1]] + DF[[vr2]] + DF[[vr3]]) }> vn <- merge(vn, d, by.x="MECH.NAME", by.y="MECH.NAME", all=T) > new <- (vr1+vr2+vr3) / vn$SUM > return(new) > > } > > # run function > > vn$PROV.PM.FBCTS <- allocation(PROV.PM.FBCTS, FBCTS.INV.TOT, FBCTS.REC.TOT) > > Here's the error: > >> >vn$PROV.PM.FBCTS <- allocation(PROV.PM.FBCTS, FBCTS.INV.TOT, > FBCTS.REC.TOT) > Error in eval(expr, envir, enclos) : object 'vr1' not found > > > # If I pull the calculations out of the function it works: > > > d <- ddply(vn, "MECH.NAME", summarise, SUM = sum(PROV.PM.FBCTS, > FBCTS.INV.TOT, FBCTS.REC.TOT))Note that sum(a,b) is not the same as a+b.> vn <- merge(vn, d, by.x="MECH.NAME", by.y="MECH.NAME", all=T) > vn$PROV.PM.FBCTS <- (PROV.PM.FBCTS + FBCTS.INV.TOT + FBCTS.REC.TOT) / > vn$SUM > >-- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University
Jennifer Sabatier
2012-Aug-08 21:09 UTC
[R] Error using ddply inside user-defined function
Thank you Brian and Dennis, for your help. Those solutions worked for a single calculation but I need to replicate it a couple hundred times. This is what worked: This is what I needed:**** ** allocation <- function(vr1, vr2, vr3)**** {**** ** d<- ave(vr1+vr2+vr3, vn$MECH.NAME <http://mech.name/>, FUN = sum) ** new <- (vr1+vr2+vr3) / d ** } ** vn$PROV.PM.FBCTS <- allocation(vn$PROV.PM.FBCTS, vn$FBCTS.INV.TOT, vn$FBCTS.REC.TOT) I have to credit this link: http://www.mail-archive.com/r-help@r-project.org/msg24108.html for inspiring the "d" line of code. Thanks, again, Jen On Tue, Aug 7, 2012 at 4:53 PM, Jennifer Sabatier < plessthanpointohfive@gmail.com> wrote:> Hi All, > > I *think* it's ddply because the function recognizes vr1, etc, in other > parts of the function. > > Here's some code: > > # create dataset > > PROV.PM.FBCTS <- c(0.00 ,0.00, 33205.19, 25994.56, 23351.37, 26959.56 > ,27632.58, 26076.24, 0.00, 0.00 , 6741.42, 18665.09 ,18129.59 ,21468.39 > ,21294.60 ,22764.82, 26076.73) > FBCTS.INV.TOT <- c(0 , 0, 958612, 487990, 413344, 573347, > 870307, 552681 , 0, 0 , 163831 , 400161 , 353000, 358322 , 489969, > 147379, 1022769) > FBCTS.REC.TOT <- c(0 , 0, 1638818 ,297119 , 299436 ,414164 , > 515735, 529001 , 0, 0 , 427341 , 625893 ,437854 , 407091, 425119 , > 8663, 0) > MECH.NAME <- c("Mechanism A","Mechanism A","Mechanism A","Mechanism > A","Mechanism A","Mechanism A","Mechanism A","Mechanism A", "Mechanism > B","Mechanism B","Mechanism B","Mechanism B","Mechanism B","Mechanism > B","Mechanism B","Mechanism B","Mechanism B",) > > vn <- data.frame(MECH.NAME, PROV.PM.FBCTS, FBCTS.INV.TOT, FBCTS.REC.TOT) > > > # create function > > allocation <- function(vr1, vr2, vr3) > { > > d <- ddply(vn, "MECH.NAME", summarise, SUM = vr1 + vr2 + vr3) > > vn <- merge(vn, d, by.x="MECH.NAME", by.y="MECH.NAME", all=T) > new <- (vr1+vr2+vr3) / vn$SUM > return(new) > > } > > # run function > > vn$PROV.PM.FBCTS <- allocation(PROV.PM.FBCTS, FBCTS.INV.TOT, FBCTS.REC.TOT) > > Here's the error: > > > vn$PROV.PM.FBCTS <- allocation(PROV.PM.FBCTS, FBCTS.INV.TOT, > FBCTS.REC.TOT) > Error in eval(expr, envir, enclos) : object 'vr1' not found > > > # If I pull the calculations out of the function it works: > > > d <- ddply(vn, "MECH.NAME", summarise, SUM = sum(PROV.PM.FBCTS, > FBCTS.INV.TOT, FBCTS.REC.TOT)) > vn <- merge(vn, d, by.x="MECH.NAME", by.y="MECH.NAME", all=T) > vn$PROV.PM.FBCTS <- (PROV.PM.FBCTS + FBCTS.INV.TOT + FBCTS.REC.TOT) / > vn$SUM > > > > vn > MECH.NAME PROV.PM.FBCTS FBCTS.INV.TOT FBCTS.REC.TOT SUM > 1 Mechanism A 0.00000000 0 0 7713774 > 2 Mechanism A 0.00000000 0 0 7713774 > 3 Mechanism A 0.34103091 958612 1638818 7713774 > 4 Mechanism A 0.10515004 487990 297119 7713774 > 5 Mechanism A 0.09543077 413344 299436 7713774 > 6 Mechanism A 0.13151418 573347 414164 7713774 > 7 Mechanism A 0.18326628 870307 515735 7713774 > 8 Mechanism A 0.14360783 552681 529001 7713774 > 9 Mechanism B 0.00000000 0 0 5402533 > 10 Mechanism B 0.00000000 0 0 5402533 > 11 Mechanism B 0.11067280 163831 427341 5402533 > 12 Mechanism B 0.19337580 400161 625893 5402533 > 13 Mechanism B 0.14974155 353000 437854 5402533 > 14 Mechanism B 0.14565046 358322 407091 5402533 > 15 Mechanism B 0.17332290 489969 425119 5402533 > 16 Mechanism B 0.03309685 147379 8663 5402533 > 17 Mechanism B 0.19413964 1022769 0 5402533 > > > Can anyone help me figure out why I can't put this calculation in a > function? Is this a systemic thing I should look for in the future? > > I know there are other ways to do what I want, but I am not very curious > about this. > > Best, > > Jen > >[[alternative HTML version deleted]]