[R] How to DataFrame[..==..] <- NA ?

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Feb 21 09:44:35 CET 2001


On Wed, 21 Feb 2001, Christian Hoffmann wrote:

> Hi all,
>
> I have a dat frame U2
> > dim(U2)
> 1442  174
>
> I am stuck at a point which S-Plus3.4 went smoothly. In R-1.2.1 (compiled
> on Solaris5) I am getting:
>
> > U2[U2 == -9] <- NA
>  Error in [<-.data.frame(*tmp*, U2 == -9, value = NA) :
>  matrix subscripts not allowed in replacement

What do you actually want to do?  A single index to a data frame is
indexing the columns, not the entries.  Unlike a matrix, a data frame
cannot be indexed as if its entries were one long vectors (as they are
not).

If I guess that this is a data frame with only numeric entries and you
want to set any entry which is equal to -9 to NA, then you could

(1) convert it to a numeric matrix and do what you have been trying, then
convert back (if needed).

(2) process it a column at a time, something like

change9 <- function(x) ifelse(x==-9, NA, x)
for(i in 1:174) if(is.numeric(U2[[i]])) U2[[i]] <- change9(U2[[i]])

This will work for general data frames. (You could use lapply, chase up
the row names, ..., but it is unlikely to be appreciably faster.)

Given the sizes, I guess (1) is preferable.

Or

(3) do the conversion when you input the data, e.g. use na.strings in
read.table,

>
> I tried some work-arounds:
>
> > U2[which(U2 == -9)] <- NA
>   Error in [<-.data.frame(*tmp*, which(U2A == -9), value = NA) :
>  new columns would leave holes after existing columns
>
> > U2[which(U2 == -9)] <- list(NA)  # the same error
>
> With the function
> > function (arr, func, ...) array(func(as.vector(arr), ...), dim(arr),
> dimnames(arr))
>
> > U2x <- vapply(U2,function(x)  ifelse(x == -9,NA,x))
>  Error in rep(no, length = length(ans)) : Unimplemented feature in rep
>
>
> By the way:
> >length(which(U2 == -9))
> 5273
> >length(which(is.na(U2)))
> 194
>
> I would be glad to learnt what went wrong here.
>
>
> -christian
> Dr.sc.math.Christian W. Hoffmann
> Mathematics and Statistical Computing
> Landscape Modeling and Web Applications
> Swiss Federal Research Institute WSL
> Zuercherstrasse 111
> CH-8903 Birmensdorf, Switzerland
> phone: ++41-1-739 22 77    fax: ++41-1-739 22 15
> e-mail: Hoffmann at WSL.CH
> www: http://www.wsl.ch/staff/christian.hoffmann/
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list