Hello dear R-users, I have a problem in my code about ignoring NA values without removing them. I'm working on a list of files. The aim is to fill one file from another according to the highest correlation (correlation coeff between all my files, so the file which looks like the most to the one I want to fill). When I have just small gaps of NA, my function works well. The problem is when I have only NAs in some files. As a consequence, it cannot calculate any correlation coefficients (my previous function in the case of only NAs in the file returns "NA" for the correlation coefficient), and so it cannot fill it or make any calculation with it. Nevertheless in my work I need to keep these NA files in my list (and so to keep their dimensions). Otherwise it creates some dimensions problems, and my function needs to me automatic for every files. So my question in this post is: how to ignore (or do nothing with them if you prefer) NA files with NA correlation coefficients? The function for filling files (where there's the problem) is: na.fill <- function(x, y){ i <- is.na(x[1:8700,1]) xx <- y[1:8700,1] new <- data.frame(xx=xx) x[1:8700,1][i] <- predict(lm(x[1:8700,1]~xx, na.action=na.exclude), new)[i] x } My error message is: Error in model.frame.default(formula = x[1:8700, 1] ~ xx, na.action = na.exclude, : : invalid type (NULL) for variable 'xx' I tried to add in the function: ifelse( all(is.null(xx))==TRUE,return(NA),xx) or ifelse( all(is.null(xx))==TRUE,return(NULL),xx) but it still doesn't work. How can I write that in my function? With NA, NULL or in another way? Thank you very much for your answers -- View this message in context: http://r.789695.n4.nabble.com/how-to-ignore-NA-with-NA-or-NULL-tp4632287.html Sent from the R help mailing list archive at Nabble.com.
I find that avoiding using the return() function at all makes my code easier to follow. In your case it is simply incorrect, though, since ifelse is a vector function and return is a control flow function. Your code is not reproducible and your description isn't clear about how you are handling the return result from this function, so I can't be sure what you are really asking, but I suspect you just want flow control, so use (untested): na.fill <- function(x, y){ i <- is.na(x[1:8700,1]) xx <- y[1:8700,1] new <- data.frame(xx=xx) if ( !all(is.na(xx)) ) { x[1:8700,1][i] <- predict(lm(x[1:8700,1]~xx, na.action=na.exclude),new)[i] } x } --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> wrote:>Hello dear R-users, > >I have a problem in my code about ignoring NA values without removing >them. >I'm working on a list of files. The aim is to fill one file from >another >according to the highest correlation (correlation coeff between all my >files, so the file which looks like the most to the one I want to >fill). >When I have just small gaps of NA, my function works well. >The problem is when I have only NAs in some files. As a consequence, it >cannot calculate any correlation coefficients (my previous function in >the >case of only NAs in the file returns "NA" for the correlation >coefficient), >and so it cannot fill it or make any calculation with it. > >Nevertheless in my work I need to keep these NA files in my list (and >so to >keep their dimensions). Otherwise it creates some dimensions problems, >and >my function needs to me automatic for every files. > >So my question in this post is: how to ignore (or do nothing with them >if >you prefer) NA files with NA correlation coefficients? >The function for filling files (where there's the problem) is: > >na.fill <- function(x, y){ > i <- is.na(x[1:8700,1]) > xx <- y[1:8700,1] > new <- data.frame(xx=xx) > x[1:8700,1][i] <- predict(lm(x[1:8700,1]~xx, na.action=na.exclude), >new)[i] > x > } > >My error message is: Error in model.frame.default(formula = x[1:8700, >1] ~ >xx, na.action = na.exclude, : : invalid type (NULL) for variable 'xx' > >I tried to add in the function: >ifelse( all(is.null(xx))==TRUE,return(NA),xx) or >ifelse( all(is.null(xx))==TRUE,return(NULL),xx) > >but it still doesn't work. >How can I write that in my function? With NA, NULL or in another way? >Thank you very much for your answers > > >-- >View this message in context: >http://r.789695.n4.nabble.com/how-to-ignore-NA-with-NA-or-NULL-tp4632287.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >R-help at 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.
Hello, 'ifelse' is vectorized, what you want is the plain 'if'. if(all(is.na(xx))) return(NA) Hope this helps, Rui Barradas Em 04-06-2012 09:56, jeff6868 escreveu:> Hello dear R-users, > > I have a problem in my code about ignoring NA values without removing them. > I'm working on a list of files. The aim is to fill one file from another > according to the highest correlation (correlation coeff between all my > files, so the file which looks like the most to the one I want to fill). > When I have just small gaps of NA, my function works well. > The problem is when I have only NAs in some files. As a consequence, it > cannot calculate any correlation coefficients (my previous function in the > case of only NAs in the file returns "NA" for the correlation coefficient), > and so it cannot fill it or make any calculation with it. > > Nevertheless in my work I need to keep these NA files in my list (and so to > keep their dimensions). Otherwise it creates some dimensions problems, and > my function needs to me automatic for every files. > > So my question in this post is: how to ignore (or do nothing with them if > you prefer) NA files with NA correlation coefficients? > The function for filling files (where there's the problem) is: > > na.fill<- function(x, y){ > i<- is.na(x[1:8700,1]) > xx<- y[1:8700,1] > new<- data.frame(xx=xx) > x[1:8700,1][i]<- predict(lm(x[1:8700,1]~xx, na.action=na.exclude), > new)[i] > x > } > > My error message is: Error in model.frame.default(formula = x[1:8700, 1] ~ > xx, na.action = na.exclude, : : invalid type (NULL) for variable 'xx' > > I tried to add in the function: > ifelse( all(is.null(xx))==TRUE,return(NA),xx) or > ifelse( all(is.null(xx))==TRUE,return(NULL),xx) > > but it still doesn't work. > How can I write that in my function? With NA, NULL or in another way? > Thank you very much for your answers > > > -- > View this message in context: http://r.789695.n4.nabble.com/how-to-ignore-NA-with-NA-or-NULL-tp4632287.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.