[R] Trouble Using mapply

Steve Lianoglou mailinglist.honeypot at gmail.com
Sun Jul 31 18:43:57 CEST 2011


Hi,

On Sun, Jul 31, 2011 at 12:30 PM, Alex Zhang <alex.zhang at 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



More information about the R-help mailing list