[R] how to ignore NA with "NA" or "NULL"
jeff6868
geoffrey_klein at etu.u-bourgogne.fr
Mon Jun 4 17:40:33 CEST 2012
Thanks for answering Jeff.
Yes sorry it's not easy to explain my problem. I'll try to give you a
reproductible example (even if it'll not be exactly like my files), and I'll
try to explain my function and what I want to do more precisely.
Imagine for the example: df1, df2 and df3 are my files:
df1 <- data.frame(x1=c(rnorm(1:5),NA,NA,rnorm(8:10)))
df2 <- data.frame(x2=rnorm(1:10))
df3 <- data.frame(x3=c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA))
df <- list(df1,df2,df3)
I want to fill each NA gaps of my files. If I have only df1 and df2 in my
list, it'll work. If I introduce df3 (a file with only NAs), R won't
understand what to do.
In my function:
na.fill <- function(x, y){
i <- is.na(x[1:10,1])
xx <- y[1:10,1]
new <- data.frame(xx=xx)
x[1:10,1][i] <- predict(lm(x[1:10,1]~xx, na.action=na.exclude),
new)[i]
x
}
x is the file I want to fill. So "i" lists all the NA gaps of the file.
xx is the file that will be used to fill x (actually the best correlated
file with x according to all my files).
And then I apply a linear regression between my 2 files: "x" and "xx" to
take predicted values from xx to put in the gaps of x.
Before I got files containing only NAs, it was working well. But since I
introduced some files with no data and so only NAs, I have my problem.
I got different NA problems when I tried a few solutions:
Error in model.frame.default(formula = x[1:8700,1] ~xx, na.action =
na.exclude, : : invalid type (NULL) for variable 'xx' OR
0 (non-NA) cases OR
is.na() applied to non-(list or vector) of type 'NULL
Actually I'm looking for a solution in na.fill to avoid these problems, in
order to ignore these "only NA files" from the calculation (maybe something
like na.pass) but I would like to keep them in the list. So the aim would be
maybe to keep them unchanged (if I have for example ST1 file with 30 only NA
in input, I want to have ST1 file with 30 only NA in output) but calculation
should work with these kinds of files in my list even if the code does
nothing with them.
Hope you've understood. Thanks again for your help.
--
View this message in context: http://r.789695.n4.nabble.com/how-to-ignore-NA-with-NA-or-NULL-tp4632287p4632314.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list