[R] remove NaN from element in a vector in a list

David Winsemius dwinsemius at comcast.net
Tue Sep 27 22:24:11 CEST 2011


On Sep 27, 2011, at 4:02 PM, Ben qant wrote:

> Hello,
>
> What is the best way to turn a matrix into a list removing NaN's?  
> I'm new to
> R...
>
> Start:
>
>> mt = matrix(c(1,4,NaN,5,3,6),2,3)
>> mt
>     [,1] [,2] [,3]
> [1,]    1  NaN    3
> [2,]    4    5    6

 > apply(mt, 1, function(x) x[!is.nan(x)] )
[[1]]
[1] 1 3

[[2]]
[1] 4 5 6

The function is.finite would also remove infinities as well as the NaNs.


>
> Desired result:
>
>> lst
> [[1]]
> [1] 1 3
>
> [[2]]
> [1] 4 5 6
>
>
> Thanks!
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list