[R] changing the values in a list

Peter McMahan peter.mcmahan at gmail.com
Fri Jan 11 04:48:27 CET 2008


try:
res <- apply(res,2,function(x){x[is.na(x)] <- y[is.na(x)];x})

should work, though not the most efficient/elegant

alternately:
y <- matrix(rep(y,ncol(x)),nrow=nrow(y))
x[is.na(x)] <- y[is.na(x)]

(also not the most efficient/elegant)

On Jan 10, 2008, at 9:38 PM, dxc13 wrote:

>
> useR's,
>
> Does anyone know of an efficient way to change the values in a list to
> different numbers stored in an object?  For example, consider the  
> following:
>
> y <- matrix(c(20, 13, 25, 10, 17), ncol = 1)
>
>> 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
> NA
> [2,]   NA   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   NA   NA   NA   NA    NA    NA     
> NA    NA
> NA
>
> What I want to do is change the values in each column of the list  
> that are
> not NA to their corresponding Y values in the matrix given.  So any  
> real
> number in the first position of any column in the list should be  
> changed to
> 20, the second number would be changed to 13, and so on...
> Does anyone know an easy way to do this?
> Thanks in advance.
>
> Derek
> -- 
> View this message in context: http://www.nabble.com/changing-the-values-in-a-list-tp14749183p14749183.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.




More information about the R-help mailing list