s-news
[Top] [All Lists]

Re: Replacing NA with a particular value

To: "Khan, Sohail" <khan@cshl.edu>
Subject: Re: Replacing NA with a particular value
From: carr.gj@pg.com
Date: Fri, 24 Feb 2006 10:05:29 -0500
Cc: s-news@lists.biostat.wustl.edu, s-news-owner@lists.biostat.wustl.edu
Pgsenderhost: bdc-notes041.na.pg.com [155.125.116.41]

Sohail,

In case you haven't yet gotten what you need on this, the direct solution is to index the non-missing values of ratio that are neighbors to the missing values.  Calculate cumsum(!is.na(ratio)), and cumsum(!is.na(ratio))+1, and you will see that they give the index to the NON-MISSING values of ratio that neighbor your missing values.  Then ratio[!is.na(ratio)][cumsum(!is.na(ratio))] and ratio[!is.na(ratio)][cumsum(!is.na(ratio))+1] are the neighbors.  Run the following code to see the result.  This mechanism also provides a basis to do a "last value carried forward".  Minor mods would be required to handle leading/trailing NAs?

ratio<-c(.25,NA,NA,NA,.25,NA,NA,.39)

df<-data.frame(
        ratio=ratio,
        indexUp=cumsum(!is.na(ratio)),
        indexDown=cumsum(!is.na(ratio))+1)
df$ratioUp<-df$ratio[!is.na(df$ratio)][df$indexUp]
df$ratioDown<-df$ratio[!is.na(df$ratio)][df$indexDown]
df$newRatio<-ifelse((df$ratioUp-df$ratioDown)==0 & is.na(df$ratio),df$ratioUp,df$ratio)

Greg



"Khan, Sohail" <khan@cshl.edu>
Sent by: s-news-owner@lists.biostat.wustl.edu

02/22/2006 11:14 PM

       
        To:        <s-news@lists.biostat.wustl.edu>
        cc:        
        Subject:        [SPAM]::[S] Replacing NA with a particular value



Dear list,

I have a data frame as:

chr                 chr.pos                                  abs.pos                                  ratio
1                 2500                                  3600                                  0.25
1                 5600                                  7500                                  NA
2                 1450                                  5200                                  NA
2                 5000                                  7005                                  NA
2                 6480                                  8754                                  0.25
2                 5425                                  6542                                  NA
2                 6000                                  8963                                  NA
2                 6542                                  9512                                  0.32

I would like to replace the NAs only if they occur in between the same values, in the data set above replace NA between 0.25 with 0.25, and leave the NA between 0.25 and 0.32.  Any help is greatly appreciated.

-Sohail
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news


<Prev in Thread] Current Thread [Next in Thread>