[R] how to remove NaN columns ?

Jason Turner jasont at indigoindustrial.co.nz
Fri Oct 24 20:56:41 CEST 2003


ryszard.czerminski at pharma.novartis.com wrote:
> How can I remove columns with NaN entries ?
> 
> Here is my simple example:
> 
> 
>>data <- read.csv("test.csv")
>>xdata <- data[3:length(data)]
>>xs <- lapply(xdata, function(x){(x - mean(x))/sqrt(var(x))})
>>x <- data.frame(xs)
>>x
> 
>                    C     D               E                F
> 1 -0.7071068 NaN -0.7071068 -0.7071068
> 2  0.7071068 NaN  0.7071068  0.7071068
> 
> I am sure it is possible to remove column D (with NaN's) in some simple 
> fashion, using is.nan function
> without explicitly looping through, and I am sure I was able to do it in 
> the past, but I cannot recall how.
> 

In addition to Andy's helpful suggestion, if your data is a matrix 
rather than a data.frame, you can use which() with arr.ind=TRUE.  For 
this example, Andy's suggestion is cleaner, however.

 > foo <- as.matrix(foo)
 > foo
            C   D          E          F
1 -0.7071068 NaN -0.7071068 -0.7071068
2  0.7071068 NaN  0.7071068  0.7071068
 > which(is.nan(foo))
[1] 3 4
 > which(is.nan(foo),arr.ind=TRUE)
   row col
1   1   2
2   2   2
 > unique(which(is.nan(foo),arr.ind=TRUE)[,2])
[1] 2
 >

-- 
Indigo Industrial Controls Ltd.
http://www.indigoindustrial.co.nz
64-21-343-545
jasont at indigoindustrial.co.nz




More information about the R-help mailing list