# [R] substituting values

Stefano Calza stecalza at tiscali.it
Thu Mar 9 15:08:43 CET 2006

On Thu, Mar 09, 2006 at 08:44:27AM -0500, Chuck Cleland wrote:
<Chuck>X <- matrix(c(  1, 2, 3,   4, "x",
<Chuck>               "x", 2, 2,   2,   2,
<Chuck>                 2, 2, 2, "x",   2),
<Chuck>             ncol=5, byrow=TRUE)

Why not:

X[X=="x"] <- NA
mode(X) <- "numeric" ## to get a numeric matrix

Btw if you got X with read.table you can specify the missing string.

Ste

<Chuck>
<Chuck>X.new <- matrix(as.numeric(X), ncol=ncol(X))
<Chuck>
<Chuck>or
<Chuck>
<Chuck>X.new <- apply(X, 2, function(x){replace(x, x == "x", NA)})
<Chuck>
<Chuck>Stefan Semmeling wrote:
<Chuck>> dear list,
<Chuck>>
<Chuck>> i have a matrix with missing values like
<Chuck>>
<Chuck>> 1    2    3    4    x
<Chuck>> x    2    2    2    2
<Chuck>> 2    2    2    x    2
<Chuck>>
<Chuck>> the x stands for the missing value.
<Chuck>> i have to substitute it to NA
<Chuck>>
<Chuck>> substitute or replace didn?t work out, as they are for vectors only
<Chuck>>
<Chuck>> however matrix[,i] also didn?t work.
<Chuck>>
<Chuck>> can anybody tell me, who i can change these values easily?
<Chuck>>
<Chuck>> thanks stefan
<Chuck>>
<Chuck>> 	[[alternative HTML version deleted]]
<Chuck>>
<Chuck>>
<Chuck>>
<Chuck>> ------------------------------------------------------------------------
<Chuck>>
<Chuck>> ______________________________________________
<Chuck>> R-help at stat.math.ethz.ch mailing list
<Chuck>> https://stat.ethz.ch/mailman/listinfo/r-help
<Chuck>
<Chuck>--
<Chuck>Chuck Cleland, Ph.D.
<Chuck>NDRI, Inc.
<Chuck>71 West 23rd Street, 8th floor
<Chuck>New York, NY 10010
<Chuck>tel: (212) 845-4495 (Tu, Th)
<Chuck>tel: (732) 452-1424 (M, W, F)
<Chuck>fax: (917) 438-0894
<Chuck>
<Chuck>______________________________________________
<Chuck>R-help at stat.math.ethz.ch mailing list
<Chuck>https://stat.ethz.ch/mailman/listinfo/r-help