AndMan21
AndMan21

Reputation: 533

How to pass multiple arguments to existing functions while using ddply?

I see a lot of similar questions, but nothing that quite hints how to tackle my specific problem. I have a data frame (nicely melted) that includes two factor columns and two columns with which I want to feed a function that has two arguments. I'd like to do basically what ddply does, in that it would feed the two arguments to the function for each of the two factor combinations. In my case, the second argument to the function "ros" (package "CensReg") is of the class "logical", so I created a dummy set of True or False tags to go along with the numerical data.

df <- data.frame(f1=c(rep("A",5), rep("B",5), rep("C",5)), f2=c(rep("Yes",10), rep("No",5)), d1=c(2,2,2,rnorm(12,9)), d2=(c(rep("TRUE",3),rep("FALSE",12))=="TRUE"))

I'm trying to do something like this:

ddply(.data = df, .variables = .(f1, f2), function(x,y) ros(d1, d2))

What am I missing about ddply that would prevent this from happening? I get the error "Error: object 'd1' not found". The class of the output of the ros function is listed as such:

class(ros(x,y))
[1] "ros" "lm" 

If that's messing up the ddply, I can see extracting what I need from the function, similar to extracting coefficients from linear regression:

ddply(.data = df, .variables = .(f1, f2), function(x,y) mean = mean(ros(d1, d2)))

(Just trust me that you can call mean(ros(.....)) and return a value; basically it's doing a robust regression on data sets that have "censored" data points (below a testing detection limit), and lets you calculate summary statistics that you can call manually using "mean", "median", etc.)

Any guidance as how to pass the two d1 and d2 arguments to a function within ddply can proceed would be greatly appreciated!

Upvotes: 0

Views: 2164

Answers (1)

agstudy
agstudy

Reputation: 121568

Either use summarize or transform without a nested function :

ddply(.data = df, .variables = .(f1, f2),summarize,ros(d1,d2))

Or if you want to use a nested function, you should create one with a single parameter (data.frame) that contain the grouped element:

ddply(.data = df, .variables = .(f1, f2),function(x)ros(x$d1,x$d2))

One hint to demystify this is to use browser to inspect araguments:

ddply(.data = df, .variables = .(f1, f2),function(x)browser())

Now, if you inspect x, you can verify that it is a data.frame :

Browse[1]> x

#    f1  f2       d1    d2
# 1  A Yes 2.000000  TRUE
# 2  A Yes 2.000000  TRUE
# 3  A Yes 2.000000  TRUE
# 4  A Yes 7.448215 FALSE
# 5  A Yes 8.599762 FALSE

Upvotes: 1

Related Questions