[R] using lapply()

dxc13 dxc13 at health.state.ny.us
Tue Jan 8 18:11:23 CET 2008


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.




More information about the R-help mailing list