Try this. check.na <- function(mat){ nas <- NULL for(st in seq.int(ncol(mat))) if(sum(is.na(mat[, st])) == nrow(mat) - 1) nas <- c(nas, st) if(length(nas)){ mat <- mat[, -nas] mat <- mat[-nas, ] } mat } check.na(df1) file1 file3 file1 1.0 0.8 file3 0.8 1.0
Note that you must remove both the columns and the rows, it's a correlation matrix. And that's also why the 'ncol(mat) minus 1', the diagonal value need not be NA. Rui Barradas jeff6868 wrote > > Hello Rui, > > Thanks for your answer too. > I tried your proposition too, but by giving the value 0 for this file, it > still wants to make a calculation with it. As it is looking for the best > correlation, and then the 2nd best correlation, giving only 0 seems to be > a problem for the 2nd best correlation at least. > Maybe the best way to solve the problem would be to introduce in the > function get.max.cor a line which would delete all the colums containing > only NAs in my correlation matrix? > For example if my calculated correlation matrix is (imagine that the > numeric values are correlation coefficients for the example): > > x <- data.frame(a = 1:10, b = c(1:5,NA,7:9, NA), c = 21:30, d = NA) > > Maybe is it possible in my function to delete only columns containing 100% > of NA, in order to have a matrix like this: > > x <- data.frame(a = 1:10, b = c(1:5,NA,7:9, NA), c = 21:30) > > and to keep other columns even if there're some NAs (the calculation is > still possible as they're numeric coefficients in the column). > Actually, it cannot look for the best or the second best correlation > coefficient in a column if it contains only NA. > I think that a correlation matrix like this would allow the calculation > for the next function and the rest of my script. > -- View this message in context: http://r.789695.n4.nabble.com/Prevent-calculation-when-only-NA-tp4630716p4630732.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.