[R] Using ddply within a function by argument transfer
Joshua Wiley
jwiley.psych at gmail.com
Sun Mar 4 06:39:44 CET 2012
Hi Fredrik,
A reproducible example would help. We have neither your data nor your
functions.
It is not clear to me what your problem is; I have no difficulty
passing arguments from a higher function to ddply().
mtcars[1,1] <- NA
f <- function(data, factors, f1) {
ddply(.data = data, as.quoted(factors), .fun = colMeans, na.rm =
ifelse(f1, FALSE, TRUE))
}
ddply(.data = mtcars, .(vs), .fun = colMeans, na.rm = ifelse(TRUE, FALSE, TRUE))
f(mtcars, "vs", f1 = TRUE)
f(mtcars, "vs", f1 = FALSE)
Cheers,
Josh
On Sat, Mar 3, 2012 at 9:20 PM, Fredrik Karlsson <dargosch at gmail.com> wrote:
> Hi Michael,
>
> No, sorry - that is neither the problem or the solution.
>
>> suspicious.vowels(pb,c("Type","Sex","Vowel"),F1,F2)
> Error in mean(y, na.rm = na.rm) : object 'f1' not found
>
>
> /Fredrik
>
> On Sat, Mar 3, 2012 at 7:04 PM, R. Michael Weylandt <
> michael.weylandt at gmail.com> wrote:
>
>> Untested, but it might be simpler than that:
>>
>> suspicious.vowels(pb,c("Type","Sex","Vowel"),"F1",F2)
>>
>> Note that "F1" is in quotes but F2 isn't.
>>
>> Michael
>>
>> On Sat, Mar 3, 2012 at 5:46 PM, Fredrik Karlsson <dargosch at gmail.com>
>> wrote:
>> > Dear list,
>> >
>> > Sorry, but I cannot get my head around how and I could pass arguments
>> along
>> > to high-level functions. What I have is a function that would benefit
>> from
>> > me using ddply from the plyr package.
>> > However, I cannot get the arguments passing part right.
>> >
>> > So, this is my function:
>> >
>> >> suspicious.vowels <-
>> > function(data,factors,f1,f2,evaluate.original.params=TRUE) {
>> >
>> > datOrig <- ddply(.data=data,
>> > .variables=as.quoted(factors),
>> > .fun=summarize,
>> > norm=vector.space(f1,f2)[["Vector norms"]]
>> > )
>> >
>> >
>> > print(datOrig)
>> >
>> > }
>> >
>> > Of course, if I try to call this function, I get an error message telling
>> > me that the "f1" argument does not exist:
>> >
>> >> suspicious.vowels(pb,c("Type","Sex","Vowel"),"F1",F2)
>> > Error in mean(y, na.rm = na.rm) : object 'f1' not found
>> >
>> > However, the corresponding ddply call, when called from the console, does
>> > work:
>> >
>> >>
>> >
>> head(ddply(pb,as.quoted(c("Type","Sex","Vowel")),summarize,norm=vector.space(F1,F2)[["Vector
>> > norms"]]),4)
>> > Type Sex Vowel norm
>> > 1 c f aa 250.1570
>> > 2 c f aa 497.2711
>> > 3 c f aa 172.3108
>> > 4 c f aa 109.4464
>> > ...
>> >
>> > So, how do I modify the function to pass the arguments that I supply
>> > correctly? I cannot get my head around this enough to find the correct
>> > combination of deparse /substitute /... to get this right.
>> >
>> > I would be thankful for all the help I could get on this.
>> >
>> > /Fredrik
>> >
>> > --
>> > "Life is like a trumpet - if you don't put anything into it, you don't
>> get
>> > anything out of it."
>> >
>> > [[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.
>>
>
>
>
> --
> "Life is like a trumpet - if you don't put anything into it, you don't get
> anything out of it."
>
> [[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.
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/
More information about the R-help
mailing list