Re: [R] mapply

From: Gabor Csardi <csardi_at_rmki.kfki.hu>
Date: Sun, 23 Mar 2008 16:35:23 +0100

On Sun, Mar 23, 2008 at 11:06:05AM -0400, Mark Leeds wrote:
> In an earlier post, a person wanted to divide each of the rows of
>
> rawdata by the row vector sens so he did below but didn't like it and
>
> asked if there was a better solution.
>
>
>
> rawdata <- data.frame(rbind(c(1,2,2), c(4,5,6))) sens <- c(2,4,6)
>
>
>
> temp <- t(rawdata)/sens
>
> temp <- t(temp)
>
> print(temp)
>
>
>
> Gabor sent three other solutions and I understood 2 of them but not the one
> below.
>
>
>
> I think I understand mapply a little but what I don't understand how
>
> it knows to take the rows of rawdata and then I guess recycle sens ?
>
> how did the mapply know not to take the columns of rawdata and do
>
> something to them ? or maybe mapply does things element by element and it is
> doing more
>
> complex recycling ? I guess I don't really understand mapply that well but I
> did
>
> read the help of it.
>
>
>
> Thanks so much for any enlightenment from anyone besides Gabor. I bother him
> enough already
>
> and he does more than enough.
>
>
>
> tempc <- data.frame(mapply("/", rawdata, sens))
>
> print(tempc)

Mark, there is no recycling here. rawdata[1] is the first column of the data frame, rawdata[2] is the second, etc. and the mapply construct is just calculating

rawdata[1] / sens[1]
rawdata[2] / sens[2]
rawdata[3] / sens[3]

data.frame() is only needed because the result of mapply would be a matrix otherwise.

(the other (?)) Gabor

>
>
>
> Mark
>
>
>
>
>
>
>
>
>
>
>
>
> [[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.

-- 
Csardi Gabor <csardi_at_rmki.kfki.hu>    UNIL DGM

______________________________________________
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.
Received on Sun 23 Mar 2008 - 15:40:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 23 Mar 2008 - 16:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive