[R] na.pass

jim holtman jholtman at gmail.com
Mon Oct 13 14:02:13 CEST 2008


If you want to remove the "N", then you can work with the indices:

> x
 [1] NA  "B" NA  "B" "B" NA  "N" "A" "B" "B" "A" NA  "A" "N" "N" "N"
"A" "B" "B" "A"
> # if you want the indices of the non-"N", then
> (indx <- which(is.na(x) | x != "N"))
 [1]  1  2  3  4  5  6  8  9 10 11 12 13 17 18 19 20
> x[indx]
 [1] NA  "B" NA  "B" "B" NA  "A" "B" "B" "A" NA  "A" "A" "B" "B" "A"
>


On Mon, Oct 13, 2008 at 7:48 AM, Laura Bonnett
<l.j.bonnett at googlemail.com> wrote:
> I have a data frame.  It has lots of patient information, their age, their
> gender, etc etc.  I need to keep all this information whilst selecting
> relevant rows.  So, in the example of code I provided I want to remove all
> those patients who have entry N in the column with.Wcode.  The dimension of
> the data is 378 i.e. 378 patients and currently I am replacing any entries
> in column with.Wcode with the letter O as this is another level of the same
> column.  Does that make more sense?
> 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,])
> }
>
> How can I therefore not replace NA with level O but instead, ignore the NAs
> and effectively gloss over them?
>
> Thank you,
>
> Laura
>
>
> On Mon, Oct 13, 2008 at 12:42 PM, jim holtman <jholtman at gmail.com> wrote:
>>
>> 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?
>
>



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