[R] how to ignore NA with "NA" or "NULL"
Rui Barradas
ruipbarradas at sapo.pt
Mon Jun 4 17:05:58 CEST 2012
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.
More information about the R-help
mailing list