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.
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.
use na.strings = ''> x <- 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", as.is = TRUE, na.strings = '')> xV1 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> dput(x)structure(list(V1 = c(635L, 636L, 637L, 638L, 639L, 782L, 783L, 784L, 785L), V2 = c("LA", "LA", "LA", "LA", "LA", "NA", "NA", "NA", "NA"), V3 = c(201207L, 201208L, 201209L, 201210L, 201211L, 200701L, 200702L, 200703L, 200704L), V4 = c(557329L, 683771L, 613851L, 764217L, 212897L, 875634L, 614856L, 521520L, 1406400L )), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c(NA, -9L)) On Sun, Nov 18, 2012 at 4:18 PM, eric <ericstrom at aol.com> wrote:> 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
I inserted na.strings='' and that seemed to work except for a problem with a plot statement plot(x1$NA,type='l',ylab='M kg/ y ',xlab='') Error: unexpected numeric constant in "plot(x1$NA"> tail(x1)AP EU LA NA total Jun 2012 2.32 2.26 5.38 13.74 23.70 Jul 2012 2.46 2.21 5.33 12.94 22.94 Aug 2012 2.69 2.24 5.28 13.32 23.54 Sep 2012 2.62 2.28 5.14 12.99 23.06 Oct 2012 2.61 2.27 5.31 12.59 22.80 Nov 2012 2.55 2.18 5.08 12.56 22.39> str(x1)?zoo? series from Dec 2007 to Nov 2012 Data: num [1:60, 1:5] 2.02 1.91 1.79 1.66 1.37 1.25 1.32 1.31 1.32 1.31 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:5] "AP" "EU" "LA" "NA" ... Index: Class 'yearmon' num [1:60] 2008 2008 2008 2008 2008 ... What's wrong with my plot statement ? -- View this message in context: http://r.789695.n4.nabble.com/Replace-NA-with-something-else-tp4649974p4649988.html Sent from the R help mailing list archive at Nabble.com.