Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Passing parameters from a wrapper function to internal functions

I am not surprised that this function doesn't work, but I cannot quite understand why.

computeMeans <- function(data,dv,fun) {
    x <- with(data,aggregate(dv,
        list(
            method=method,
            hypo=hypothesis,
            pre.group=pre.group,
            pre.smooth=pre.smooth
        ),
        fun ) )
    return(x)
}

computeMeans(df.basic,dprime,mean)

Where df.basic is a dataframe with factors method, hypothesis, etc, and several dependent variables (and I specify one with the dv parameter, dprime).

I have multiple dependent variables and several dataframes all of the same form, so I wanted to write this little function to keep things "simple". The error I get is:

Error in aggregate(dv, list(method = method, hypo = hypothesis, 
pre.group = pre.group,  : 
    object 'dprime' not found

But dprime does exist in df.basic, which is referenced with with(). Can anyone explain the problem? Thank you!

EDIT: This is the R programming language. http://www.r-project.org/

like image 779
Chris Cox Avatar asked Feb 23 '26 12:02

Chris Cox


1 Answers

Although dprime exists in df.basic, when you call it at computeMeans it has no idea what you are referring to, unless you explicitly reference it.

computeMeans(df.basic,df.basic$dprime,mean)

will work.

like image 171
nograpes Avatar answered Feb 25 '26 00:02

nograpes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!