Steve,

Thank you very much for your response.

The method you suggested definitely helps. But the "myfun" I gave is just a dummy function to show the problem. In some parts of the work I have at hand right now, I do need the dataframe structure (i.e. all.data) to be inside of the function because the logic inside is kind of complicated and I need to use multiple columns to get the result.

A "hack" way I just came up with is to make the data.frame argument optional.

myfun <- function(threshold, all.data = data2use) {
##Just a demostration of a function that takes a dataframe.
#browser()
#print(all.data)
return (min(subset(all.data, id > threshold)$val))
}

data2use = data.frame(id = (1:10), val = (-3:6))
result1 = mapply(myfun, (2:4))

data2use = data.frame(id = (2:20), val = (-6:12))
result2 = mapply(myfun, (10:14), )  
But this is sorta secretive and is not as flexible enough when I need to get multiple data.frames into the function.

 Is there a way to get the "rep" part return what I want? Thx,

- Alex


________________________________
From: Steve Lianoglou <mailinglist.honeypot@gmail.com>
To: Alex Zhang <alex.zhang@ymail.com>
Cc: "r-help@r-project.org" <r-help@r-project.org>
Sent: Sunday, July 31, 2011 12:43 PM
Subject: Re: [R] Trouble Using mapply

Hi, rep(my

On Sun, Jul 31, 2011 at 12:30 PM, Alex Zhang <alex.zhang@ymail.com> wrote:
> Dear all,
>
> I am having a problem with mapply. I guess the reason is that mapply is not "vectorized". But could you please take a look at my code below and help me to find a solution (either a better way to use mapply or a different function to call). Thanks a lot!
>
>
> ##beginning of my code
> myfun <- function(threshold, all.data) {
> ##Just a demostration of a function that takes a dataframe.
> #browser()
> #print(all.data)
> return (min(subset(all.data, id > threshold)$val))
> }
>
> my.data = data.frame(id = (1:10), val = (-3:6))
>
> print(myfun(2, my.data)) ##Everything works up to here.
> result = mapply(myfun, (2:4), rep(my.data, 3)) ##got trouble here.
> ##More specifically, the all.data inside myfun is no longer a dataframe.

Right.

Try doing `rep(my.data, 3)` just in your workspace and see what you
get -- this is why it's not working.

In this case, though, it doesn't seem as if you need mapply, since you
aren't "looping" over two variables, simultaneously, right?. It looks
like your `threshold` is the only thing that's changing, while
`all.data` is fixed, no?

So why not just do something like:

R> lapply(thresholds, function(x, subset(all.data, id > x)$val))

Would that do the trick?

-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
	[[alternative HTML version deleted]]

