[R] Prevent calculation when only NA

Rui Barradas ruipbarradas at sapo.pt
Mon May 21 11:42:28 CEST 2012


Hello,

Maybe the function could return a special value, such as zero.
Since a column with that number doesn't exist, the code executed afterward
would simply move on to the second greatest correlation.
The function would then become

get.max.cor <- function(station, mat){
      mat[row(mat) == col(mat)] <- -Inf
	if(sum(is.na(mat[station, ])) == ncol(mat) - 1)
		0
	else
      	which( mat[station, ] == max(mat[station, ], na.rm=TRUE) )
}

df1 <- read.table(text="
          file1 file2 file3
file1    1       NA    0.8
file2    NA     1     NA  
file3   0.8     NA     1
", header=TRUE)

get.max.cor("file2", df1)


Hope this helps,

Rui Barradas

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) )
>      }
> 


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



More information about the R-help mailing list