[R] Replace <NA> with something else

arun smartpink111 at yahoo.com
Sun Nov 18 22:41:58 CET 2012


Hi,
Try this:
If you wanted to replace "<" and ">" from NA:
dat1<-read.table(text="
635    LA 201207  557329
636    LA 201208  683771
637    LA 201209  613851
638    LA 201210  764217
639    LA 201211  212897
782  <NA> 200701  875634
783  <NA> 200702  614856
784  <NA> 200703  521520
785  <NA> 200704 1406400
",sep="",header=FALSE,stringsAsFactors=FALSE)


dat1$V2<-gsub("<(.*)>","\\1",dat1$V2)
dat1
#   V1 V2     V3      V4
#1 635 LA 201207  557329
#2 636 LA 201208  683771
#3 637 LA 201209  613851
#4 638 LA 201210  764217
#5 639 LA 201211  212897
#6 782 NA 200701  875634
#7 783 NA 200702  614856
#8 784 NA 200703  521520
#9 785 NA 200704 1406400

 is.na(dat1$V2)
#[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE


#If you wanted to change "NA" to "NAM"
dat1$V2[grep("NA",dat1$V2)]<-"NAM"
A.K.



----- Original Message -----
From: eric <ericstrom at aol.com>
To: r-help at r-project.org
Cc: 
Sent: Sunday, November 18, 2012 4:18 PM
Subject: [R] Replace <NA> with something else

I am reading some data into R from an Excel spreadsheet using read.csv. Some
of the original data that comes into column 1 from the spreadsheet is text
that says NA. The NA stands for north america. As it comes in, R converts
the NA over to  <NA>. 

What is the cleanest way to change the <NA>  values to something else. In
other words, get rid of the brackets ? Maybe convert <NA> to NAM. 

635     LA 201207  557329
636     LA 201208  683771
637     LA 201209  613851
638     LA 201210  764217
639     LA 201211  212897
782   <NA> 200701  875634
783   <NA> 200702  614856
784   <NA> 200703  521520
785   <NA> 200704 1406400



--
View this message in context: http://r.789695.n4.nabble.com/Replace-NA-with-something-else-tp4649974.html
Sent from the R help mailing list archive at Nabble.com.

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





More information about the R-help mailing list