[R] na.pass

jim holtman jholtman at gmail.com
Mon Oct 13 13:42:55 CEST 2008


Not sure exactly what you are trying to do since you did not provide
commented, minimal, self-contained, reproducible code.  Let me take a
guess in that you also have to test for NAs:

> x <- sample(c("N", "A", "B", NA), 20, TRUE)
> x
 [1] "A" "A" "B" NA  "N" NA  NA  "B" "B" "N" "N" "N" "B" "A" NA  "A"
"B" NA  "A" NA
> x != "N"
 [1]  TRUE  TRUE  TRUE    NA FALSE    NA    NA  TRUE  TRUE FALSE FALSE
FALSE  TRUE  TRUE    NA  TRUE  TRUE    NA
[19]  TRUE    NA
> x[x != "N"]
 [1] "A" "A" "B" NA  NA  NA  "B" "B" "B" "A" NA  "A" "B" NA  "A" NA
> (!is.na(x)) & (x != "N")
 [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE
[19]  TRUE FALSE
> x[(!is.na(x)) & (x != "N")]
 [1] "A" "A" "B" "B" "B" "B" "A" "A" "B" "A"
>


On Mon, Oct 13, 2008 at 7:15 AM, Laura Bonnett
<l.j.bonnett at googlemail.com> wrote:
> Hi All,
>
> I have a data frame which has columns comprised mainly of "NA"s.  I know
> there are functions na.pass and na.omit etc which can be used in these
> situations however I can't them to work in this case.  I have a function
> which returns the data according to some rule i.e. removal of N in this
> code:
>
> nep <- function(data)
>    {
>    dummy <- rep(0,378)
>    for(i in 1:378){
>    if(is.na(data$with.Wcode)[i])
>    data$with.Wcode[i] <- "O"
>    }
>    for(i in 1:378){
>    if(data$with.Wcode[i]=="N")
>    dummy[i] <- i
>    }
>    return(data[-dummy,])
>    }
>
> However, I really don't want to replace the NAs with "O".  I'd just like to
> gloss over them.  I can't just delete them because the structure of the data
> frame needs to be maintained.  Can anyone suggest how I can write in a line
> or two to ignore the NAs instead of replacing them?  I've tried this code
> but it doesn't work!
>
> nep <- function(data)
>    {
>    dummy <- rep(0,378)
>    for(i in 1:378){
>    na.pass(data$with.Wcode[i])
>    if(data$with.Wcode[i]=="N")
>    dummy[i] <- i
>    }
>    return(data[-dummy,])
>    }
>
>
> Thank you,
>
> Laura
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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 that you are trying to solve?



More information about the R-help mailing list