[R] mapply

Gabor Csardi csardi at rmki.kfki.hu
Sun Mar 23 16:35:23 CET 2008


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



More information about the R-help mailing list