Re: [R] looking for the na.omit equivalent for a matrix of characters

From: Stephen Tucker <brown_emu_at_yahoo.com>
Date: Mon, 28 May 2007 21:33:26 -0700 (PDT)


You can also use type.convert() if you did want to convert your characters to numbers and "NA"'s to NA's so that you can use na.omit().

> x <- matrix("0",5,5)
> x[1,3] <- x[4,4] <- "NA"
> newx <- apply(x,2,type.convert)
> newx

     [,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

> 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_at_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_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.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
> [[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.
>
       

____________________________________________________________________________________Sick sense of humor? Visit Yahoo! TV's 
Comedy with an Edge to see what's on, when.

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. Received on Tue 29 May 2007 - 04:57:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 May 2007 - 06:31:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.