s-news
[Top] [All Lists]

Re: Replacing NA with a particular value

To: "Khan, Sohail" <khan@cshl.edu>, S-news <s-news@lists.biostat.wustl.edu>
Subject: Re: Replacing NA with a particular value
From: Tony Plate <tplate@blackmesacapital.com>
Date: Thu, 23 Feb 2006 15:10:24 -0700
In-reply-to: <C8696843AE995F4EA4CDC3E2B83482A901878F81@mailbox02.cshl.edu>
References: <C8696843AE995F4EA4CDC3E2B83482A901878F81@mailbox02.cshl.edu>
User-agent: Mozilla Thunderbird 1.0.5 (Windows/20050711)
Here's a another approach using approx. If you want to consider slightly different values of 'ratio' to be equal, then modify the comparison yleft==yright appropriately.

> pok <- read.table(header=T, clipboardConnection())
> pok2 <- na.exclude(data.frame(ratio=pok$ratio, n=seq(len=nrow(pok))))
> yleft <- approx(y=pok2$ratio, x=pok2$n, xout=seq(len=nrow(pok)), method="constant", f=0)$y > yright <- approx(y=pok2$ratio, x=pok2$n, xout=seq(len=nrow(pok)), method="constant", f=1)$y
> yleft
[1] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.32
> yright
[1] 0.25 0.25 0.25 0.25 0.25 0.32 0.32 0.32
> cbind(pok, ratio2=ifelse(is.na(pok$ratio) & yleft==yright, yleft, pok$ratio))
  chr chr.pos abs.pos ratio ratio2
1   1    2500    3600  0.25   0.25
2   1    5600    7500    NA   0.25
3   2    1450    5200    NA   0.25
4   2    5000    7005    NA   0.25
5   2    6480    8754  0.25   0.25
6   2    5425    6542    NA     NA
7   2    6000    8963    NA     NA
8   2    6542    9512  0.32   0.32
>

Khan, Sohail wrote:
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>