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]]
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
One way: A <- matrix(round(rnorm(20)), ncol=4) A[2,1] <- "x" A[which(A == "x", arr.ind=TRUE)] <- NA A <- matrix(as.numeric(A), ncol=dim(A)[2]) Best, Matthias> -----Urspr?ngliche Nachricht----- > Von: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von > Stefan Semmeling > Gesendet: Donnerstag, 09. M?rz 2006 14:32 > An: r-help at stat.math.ethz.ch > Betreff: [R] substituting values > Vertraulichkeit: Pers?nlich > > > 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]] > >
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. ?read.table 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>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html <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 <Chuck>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html