[R] using mapply()

Dimitris Rizopoulos Dimitris.Rizopoulos at med.kuleuven.be
Tue Jan 8 21:06:00 CET 2008


do you mean the following?:

out <- rowMeans(matrix(unlist(res), ncol = length(res)))
dim(out) <- dim(res[[1]])
out


Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting dxc13 <dxc13 at health.state.ny.us>:

>
> useR's,
>
> This is a follow up question to one I previously asked.  Consider the
> 3-element list below
>
>> res
> [[1]]
>        [,1] [,2]  [,3]  [,4]  [,5] [,6]  [,7] [,8]  [,9] [,10] [,11] [,12]
> [,13] [,14]
> [1,]   NA   NA   NA 1.25 0.25 0.75   NA   NA   NA    NA    NA    NA  1.25
> 0.25
> [2,] 1.25 0.25 0.75   NA   NA   NA   NA   NA   NA  1.25  0.25  0.75    NA
> NA
> [3,]   NA   NA   NA   NA   NA   NA 1.25 0.25 0.75    NA    NA    NA    NA
> NA
> [4,]   NA   NA 1.25 0.25 0.75   NA   NA   NA   NA    NA    NA  1.25  0.25
> 0.75
> [5,]   NA   NA   NA   NA   NA 1.25 0.25 0.75   NA    NA    NA    NA    NA
> NA
>
> [[2]]
>        [,1] [,2]  [,3] [,4]  [,5] [,6]  [,7]  [,8] [,9] [,10] [,11] [,12]
> [,13] [,14]
> [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA
> 1.25
> [2,] 1.25   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA
> NA
> [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25  0.25  0.25  0.25  0.25
> 0.25
> [4,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA
> NA
> [5,]   NA   NA   NA   NA   NA  1.25   NA   NA   NA    NA    NA    NA    NA
> NA
>
> [[3]]
>        [,1] [,2]  [,3]  [,4] [,5] [,6]  [,7]  [,8] [,9] [,10] [,11] [,12]
> [,13]  [,14]
> [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA
> 1.25
> [2,] 0.25   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA
> NA
> [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25  0.25  0.25  0.25  0.25
> 0.25
> [4,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA
> NA
> [5,]   NA   NA   NA   NA   NA  0.25   NA   NA   NA    NA    NA    NA    NA
> NA
>
> What I want to do is find the values that are not NA simultaneously for each
> value in each element of the list, and then average these numbers and store
> them in a 14x1 vector.  For example, in position (2,1) in each of the 3
> elements of the res list, there is a value, 1.25 in [[1]] and [[2]], 0.25 in
> [[3]].  Since there is a value in this position in all elements of the list
> I want to average these numbers: (1.25+1.25+0.25)/3 and store this value in
> a numeric vector.   So, if there are not 3 values to average, this means
> that a given position (i,j) does not have a value in each element of the
> list and thus I want to store NA in the 14x1 vector.  In the end, the 14x1
> vector will be mixed with values and NA's or NaN's.
>
> Does anyone know a efficient way to do this?  Maybe using mapply()?  I hope
> it is clear.  Thank you for any input.
>
> Derek
> --
> View this message in context:   
> http://www.nabble.com/using-mapply%28%29-tp14697351p14697351.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
>



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm




More information about the R-help mailing list