Re: [R] changing the values in a list

From: Peter McMahan <peter.mcmahan_at_gmail.com>
Date: Thu, 10 Jan 2008 21:48:27 -0600

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.



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. Received on Fri 11 Jan 2008 - 03:51:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Jan 2008 - 04:30:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive