[R] numeric variables converted to character when recoding missingvalues

Juan Pablo Lewinger lewinger at usc.edu
Sat Jun 24 08:14:12 CEST 2006


Thanks Bert, that works of course and is much more straightforward than what
I was trying. However, I'm still puzzled as to why x[x==99]<-NA works (i.e.
it replaces the 999s with NAs and keeps the numeric variables numeric) but
is.na(x[x==999])<-TRUE doesn't (it replaces the 999s with NAs but changes
all variables where a replacement was made to character)

PS:  As far as I can tell section 2.5 of "An Introduction to R" -which I had
read- doesn't answer my original question.

Juan Pablo Lewinger
Department of Preventive Medicine 
Keck School of Medicine 
University of Southern California

-----Original Message-----
From: Berton Gunter [mailto:gunter.berton at gene.com] 
Sent: Friday, June 23, 2006 3:15 PM
To: 'Juan Pablo Lewinger'; r-help at stat.math.ethz.ch
Subject: RE: [R] numeric variables converted to character when recoding
missingvalues

Please read section 2.5 of "An Introduction to R". Numerical missing values
are assigned as NA:

x[x==999]<-NA

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Juan 
> Pablo Lewinger
> Sent: Friday, June 23, 2006 3:00 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] numeric variables converted to character when 
> recoding missingvalues
> 
> Dear R helpers,
> 
> I have a data frame where missing values for numeric 
> variables are coded as
> 999. I want to recode those as NAs. The following only 
> partially succeeds
> because numeric variables are converted to character in the process:
> 
> df <- data.frame(a=c(999,1,999,2), b=LETTERS[1:4])
> is.na(df[2,1]) <- TRUE
> df
> 
>     a b
> 1 999 A
> 2  NA B
> 3 999 C
> 4   2 D
> 
> is.numeric(df$a)
> [1] TRUE
> 
> 
> is.na(df[!is.na(df) & df==999]) <- TRUE
> df
>      a b
> 1 <NA> A
> 2    1 B
> 3 <NA> C
> 4    2 D
> 
> is.character(df$a)
> [1] TRUE
> 
> My question is how to do the recoding while avoiding this 
> undesirable side
> effect. I'm using R 2.2.1 (yes, I know 2.3.1 is available but 
> don't want to
> switch mid project). I'd appreciate any help.
> 
> Further details:
> 
> platform i386-pc-mingw32
> arch     i386           
> os       mingw32        
> system   i386, mingw32  
> status                  
> major    2              
> minor    2.1            
> year     2005           
> month    12             
> day      20             
> svn rev  36812          
> language R              
> 
> 
> 
> Juan Pablo Lewinger
> Department of Preventive Medicine 
> Keck School of Medicine 
> University of Southern California
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>



More information about the R-help mailing list