[R] Prevent calculation when only NA

Jim Lemon jim at bitwrit.com.au
Mon May 21 10:10:00 CEST 2012


On 05/21/2012 05:59 PM, jeff6868 wrote:
> 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) )
>       }
>
>
Hi Jeff,
Can you use:

if(any(!is.na(mat))) {
...
}

Jim



More information about the R-help mailing list