[R] Trouble Using mapply

Steve Lianoglou mailinglist.honeypot at gmail.com
Sun Jul 31 20:10:29 CEST 2011


Hi,

On Sun, Jul 31, 2011 at 1:04 PM, Alex Zhang <alex.zhang at ymail.com> wrote:
> 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?

You just need to pass in a *list* of data.frame objects into your
`mapply` call. I guess how you get this list is up to you, as it
sounds like you already have a list of *different* data.frames?

Just as a note, I don't understand what you were trying to say about
why you can't use my solution, but if you just want to use the *same*
data.frame in different ways inside your "manipulation" code, you
should still stop using `mapply`. If you change `my.data` inside your
function, it won't change the my.data data.frame in the outer scope.

Consider:

R> lapply(1:5, function(x) {
  my.data[,1] <- sample(my.data[,1])
  my.data
})

You'll see that the first column is shuffled 5 times inside the
function/loop, but the `my.data` data.frame in the global scope still
hasn't changed.

Anyway, if you still think you have to use mapply, you will have to
pass it a list of data.frame's. You can replicate the same data.frame
10 times like so:

R> all.data <- lapply(1:10, function(x) my.data)

or

R> all.data <- replicate(10, my.data, simplify=FALSE)

Then pass in `all.data` to mapply. But, again, if this actually works
for you, then you don't need to use mapply anyway for the reasons I
just stated.

HTH,
-steve

> ________________________________
> From: Steve Lianoglou <mailinglist.honeypot at gmail.com>
> To: Alex Zhang <alex.zhang at ymail.com>
> Cc: "r-help at r-project.org" <r-help at 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 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
>
>
>



-- 
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