[R] Replacing NA values in one column of a data.frame

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Aug 18 16:25:20 CEST 2009


Hi,

On Aug 18, 2009, at 10:19 AM, John Kane wrote:

> Perhaps
> testdata$onecolumn[testdata$onecolumn==NA] <- 9999

I don't think this would work -- the is.na function is the way to go:

R> a <- c(1,2,3,NA,NA,10,20,NA,NA)
R> a
[1]  1  2  3 NA NA 10 20 NA NA

R> a[a == NA] <-999
R> a
[1]  1  2  3 NA NA 10 20 NA NA

R> a[is.na(a)] <-999
R> a
[1]   1   2   3 999 999  10  20 999 999

Hope that helps,
-steve

>
> --- On Mon, 8/17/09, Steve Murray <smurray444 at hotmail.com> wrote:
>
>> From: Steve Murray <smurray444 at hotmail.com>
>> Subject: [R] Replacing NA values in one column of a data.frame
>> To: r-help at r-project.org
>> Received: Monday, August 17, 2009, 11:41 AM
>>
>> Dear all,
>>
>> I'm trying to replace NA values with -9999 in one column of
>> a data frame. I've tried using is.na and the
>> testdata[testdata$onecolumn==NA] <- 9999 approach, but
>> whilst neither generate errors, neither result in -9999s
>> appearing - the NAs remain there!
>>
>> I'd be grateful for any advice on what I'm doing wrong or
>> any other suitable approaches.
>>
>> Many thanks,
>>
>> Steve
>>
>> _________________________________________________________________
>>
>> oticons.
>>
>> ______________________________________________
>> 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.
>>
>
>
>       
> __________________________________________________________________
> [[elided Yahoo spam]]
>
> ______________________________________________
> 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.

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list