Andrew Yee
2007-May-29 02:49 UTC
[R] looking for the na.omit equivalent for a matrix of characters
I have a matrix of characters (actually numbers that have been read in as numbers), and I'd like to remove the NA. I'm familiar with na.omit, but is there an equivalent of na.omit when the NA are the actual characters "NA"? Thanks, Andrew [[alternative HTML version deleted]]
jim holtman
2007-May-29 03:31 UTC
[R] looking for the na.omit equivalent for a matrix of characters
Since they are characters you can just compare for them. You did not show what your data looks like, or what you want to do if there are "NA". Do you want the row removed? You can use 'apply' to test a row for "NA": > x <- matrix("0",5,5)> x[1,3] <- x[4,4] <- "NA" > x[,1] [,2] [,3] [,4] [,5] [1,] "0" "0" "NA" "0" "0" [2,] "0" "0" "0" "0" "0" [3,] "0" "0" "0" "0" "0" [4,] "0" "0" "0" "NA" "0" [5,] "0" "0" "0" "0" "0"> apply(x, 1, function(z) any(z == "NA"))[1] TRUE FALSE FALSE TRUE FALSE> x[!apply(x, 1, function(z) any(z == "NA")),][,1] [,2] [,3] [,4] [,5] [1,] "0" "0" "0" "0" "0" [2,] "0" "0" "0" "0" "0" [3,] "0" "0" "0" "0" "0">On 5/28/07, Andrew Yee <andrewjyee@gmail.com> wrote:> > I have a matrix of characters (actually numbers that have been read in as > numbers), and I'd like to remove the NA. > > I'm familiar with na.omit, but is there an equivalent of na.omit when the > NA > are the actual characters "NA"? > > Thanks, > Andrew > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Bill.Venables at csiro.au
2007-May-29 03:57 UTC
[R] looking for the na.omit equivalent for a matrix of characters
Surely all you need to do is change them to missing and use na.omit. e.g. x <- matrix(letters, 13, 2) x[4] <- "NA" # put one in the middle is.na(x[x == "NA"]) <- TRUE (x <- na.omit(x)) Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: (I don't have one!) Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Andrew Yee Sent: Tuesday, 29 May 2007 12:50 PM To: r-help at stat.math.ethz.ch Subject: [R] looking for the na.omit equivalent for a matrix of characters I have a matrix of characters (actually numbers that have been read in as numbers), and I'd like to remove the NA. I'm familiar with na.omit, but is there an equivalent of na.omit when the NA are the actual characters "NA"? Thanks, Andrew [[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 and provide commented, minimal, self-contained, reproducible code.