[R] using lapply()

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Tue Jan 8 18:23:04 CET 2008


try the following:

delta <- c(2.5, 2.5, 1)
v <- list(
    matrix(round(rnorm(70, 2), 2), 5, 14),
    matrix(round(rnorm(70, 2), 2), 5, 14),
    matrix(round(rnorm(70, 2), 1), 5, 14)
)

v
mapply(function(x, y){ x[x > y] <- NA; x }, v, delta, SIMPLIFY = 
FALSE)


I hope it helps.

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


----- Original Message ----- 
From: "dxc13" <dxc13 at health.state.ny.us>
To: <r-help at r-project.org>
Sent: Tuesday, January 08, 2008 6:11 PM
Subject: [R] using lapply()


>
> useR's,
>
> I am trying to find a quick way to change some values in a list that 
> are
> subject to a condition to be NA.  Consider the 3x1 matrix:
>
> delta <- matrix(c(2.5,2.5,1), nrow = 1)
>
> And consider the list named v that has 3 elements
>> v
> v[[1]]
>       [,1] [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8] [,9] [,10] 
> [,11] [,12]
> [,13] [,14]
> [1,] 4.25 3.25 2.25 1.25 0.25 0.75 1.75 2.75 3.75  4.25  3.25  2.25 
> 1.25
> 0.25
> [2,] 1.25 0.25 0.75 1.75 2.75 3.75 4.75 5.75 6.75  1.25  0.25  0.75 
> 1.75
> 2.75
> [3,] 7.25 6.25 5.25 4.25 3.25 2.25 1.25 0.25 0.75  7.25  6.25  5.25 
> 4.25
> 3.25
> [4,] 3.25 2.25 1.25 0.25 0.75 1.75 2.75 3.75 4.75  3.25  2.25  1.25 
> 0.25
> 0.75
> [5,] 6.25 5.25 4.25 3.25 2.25 1.25 0.25 0.75 1.75  6.25  5.25  4.25 
> 3.25
> 2.25
>
> v[[2]]
>       [,1]  [,2]  [,3] [,4]  [,5]  [,6]  [,7]  [,8] [,9] [,10] [,11] 
> [,12]
> [,13] [,14]
> [1,] 9.25 9.25 9.25 9.25 9.25 9.25 9.25 9.25 9.25  8.25  8.25  8.25 
> 8.25
> 8.25
> [2,] 7.25 7.25 7.25 7.25 7.25 7.25 7.25 7.25 7.25  6.25  6.25  6.25 
> 6.25
> 6.25
> [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,] 4.25 4.25 4.25 4.25 4.25 4.25 4.25 4.25 4.25  3.25  3.25  3.25 
> 3.25
> 3.25
> [5,] 6.25 6.25 6.25 6.25 6.25 6.25 6.25 6.25 6.25  5.25  5.25  5.25 
> 5.25
> 5.25
>
> v[[3]]
>      [,1] [,2]  [,3] [,4] [,5] [,6] [,7] [,8]  [,9] [,10] [,11] 
> [,12] [,13]
> [,14]
> [1,]  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5   0.5   0.5   0.5 
> 0.5
> 0.5
> [2,]  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5   1.5   1.5   1.5 
> 1.5
> 1.5
> [3,]  2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5   2.5   2.5   2.5 
> 2.5
> 2.5
> [4,]  3.5  3.5  3.5  3.5  3.5  3.5  3.5  3.5  3.5   3.5   3.5   3.5 
> 3.5
> 3.5
> [5,]  4.5  4.5  4.5  4.5  4.5  4.5  4.5  4.5  4.5   4.5   4.5   4.5 
> 4.5
> 4.5
>
> The first element of the matrix delta (2.5) corresponds to the first 
> list
> element v[[1]], and so on...
>
> What I want to do is to set any values in this list that are greater 
> than
> its corresponding delta to be NA.  For example, if any value in 
> v[[1]] is
> greater than 2.5 than set it to NA, and similarly for v[[2]].  If 
> any value
> in v[[3]] is > 1, then set it NA.
>
> Can this be done using lapply or some other quick, efficient way? 
> Hope this
> makes sense.
> Thanks,
>
> Derek
> -- 
> View this message in context: 
> http://www.nabble.com/using-lapply%28%29-tp14693921p14693921.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