[R] Prevent calculation when only NA

jeff6868 geoffrey_klein at etu.u-bourgogne.fr
Mon May 21 09:59:03 CEST 2012


Hi everybody,

I have a small question about R.
I'm doing some correlation matrices between my files. These files contains
each 4 columns of data.
These data files contains missing data too. It could happen sometimes that
in one file, one of the 4 columns contains only missing data NA. As I'm
doing correlations between the same columns of each files, I get a
correlation matrix with a column containing only NAs such like this:

          file1 file 2 file 3
file1    1       NA    0.8
file2    NA     1     NA   
file3   0.8     NA     1

For file2, I have no correlation coefficient. 
My function is looking for the highest correlation coefficient for each
file. But I have an error message due to this.
My question is: how can I say to the function: don't do any calculation if
you see only NAs for the file you're working on? The aim of this function is
to automatize this calculation for 300 files.
I tried by adding: na.rm=TRUE, but it stills wants to do the calculation for
the file containing only NAs (error: 0 (non-NA) cases).
Could you tell me what I should add in my function? Thanks a lot!

get.max.cor <- function(station, mat){
        mat[row(mat) == col(mat)] <- -Inf
        which( mat[station, ] == max(mat[station, ], na.rm=TRUE) )
     }





--
View this message in context: http://r.789695.n4.nabble.com/Prevent-calculation-when-only-NA-tp4630716.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list