Skip to content
Prev 295145 / 398506 Next

Prevent calculation when only NA

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
--
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.