[R] substituting values

Chuck Cleland ccleland at optonline.net
Thu Mar 9 14:44:27 CET 2006


X <- matrix(c(  1, 2, 3,   4, "x",
               "x", 2, 2,   2,   2,
                 2, 2, 2, "x",   2),
             ncol=5, byrow=TRUE)

X.new <- matrix(as.numeric(X), ncol=ncol(X))

or

X.new <- apply(X, 2, function(x){replace(x, x == "x", NA)})

Stefan Semmeling wrote:
> dear list,
>  
> i have a matrix with missing values like
>  
> 1    2    3    4    x
> x    2    2    2    2
> 2    2    2    x    2
>  
> the x stands for the missing value.
> i have to substitute it to NA
>  
> substitute or replace didn´t work out, as they are for vectors only
>  
> however matrix[,i] also didn´t work.
>  
> can anybody tell me, who i can change these values easily?
>  
> thanks stefan
> 
> 	[[alternative HTML version deleted]]
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> 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

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list