[R] na.approx and columns with NA's

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 27 20:58:23 CEST 2007


na.approx uses approx and has the same behavior as it.  Try this:

> library(zoo)
>
> # test data
> z <- zoo(matrix(1:24, 6))
> z[,2:3] <- NA
> z[1, 2] <- 3
> z[2, 1] <- NA
> z

1  1  3 NA 19
2 NA NA NA 20
3  3 NA NA 21
4  4 NA NA 22
5  5 NA NA 23
6  6 NA NA 24
>
> # TRUE for each column that has more than 1 non-NA
> idx <- colSums(!!z, na.rm = TRUE) > 1
> idx
[1]  TRUE FALSE FALSE  TRUE
>
> z[,idx] <- na.approx(z[,idx])
> z

1 1  3 NA 19
2 2 NA NA 20
3 3 NA NA 21
4 4 NA NA 22
5 5 NA NA 23
6 6 NA NA 24


On 5/27/07, antonio rodriguez <antonio.raju at gmail.com> wrote:
> Hi,
>
> I have a object 'zoo':
>
> dim(zz)
> [1]  720 5551
>
> where some columns only have NA's values (representing land data in a
> sea surface temperature dataset) I find straightforward the use of
> 'na.approx' for individual columns from the zz matrix, but when applied
> to the whole matrix:
>
> zz.approx<-na.approx(zz)
> Erro en approx(along[!na], y[!na], along[na], ...) :
>        need at least two non-NA values to interpolate
>
> The message is clear, but how do I could skip those 'full-NA's' columns
> from the interpolation in order to perform the analysis over the columns
> which represent actual data with some NA's values
>
> Best regards,
>
> Antonio
>
> --
> =====
> Por favor, si me mandas correos con copia a varias personas,
> pon mi dirección de correo en copia oculta (CCO), para evitar
> que acabe en montones de sitios, eliminando mi privacidad,
> favoreciendo la propagación de virus y la proliferación del SPAM. Gracias.
> -----
> If you send me e-mail which has also been sent to several other people,
> kindly mark my address as blind-carbon-copy (or BCC), to avoid its
> distribution, which affects my privacy, increases the likelihood of
> spreading viruses, and leads to more SPAM. Thanks.
> =====
> Antes de imprimir este e-mail piense bien si es necesario hacerlo: El medioambiente es cosa de todos.
> Before printing this email, assess if it is really needed.
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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