[R] Replace <NA> with something else

arun smartpink111 at yahoo.com
Mon Nov 19 03:04:01 CET 2012


Hi,
x1<-read.table(text="
 Year       AP  EU  LA  NA total
Jun2012 2.32 2.26 5.38 13.74 23.70
Jul2012 2.46 2.21 5.33 12.94 22.94
Aug2012 2.69 2.24 5.28 13.32 23.54
Sep2012 2.62 2.28 5.14 12.99 23.06
Oct2012 2.61 2.27 5.31 12.59 22.80
Nov2012 2.55 2.18 5.08 12.56 22.39
",sep="",stringsAsFactors=FALSE,header=TRUE,na.strings="")




x1
#     Year   AP   EU   LA   NA. total   #found a dot after  NA i.e. (NA.)
#1 Jun2012 2.32 2.26 5.38 13.74 23.70
#2 Jul2012 2.46 2.21 5.33 12.94 22.94

-----------------------------------------------------

library(zoo)
x2<-zoo(x1[,-1],order.by=as.yearmon(x1$Year,format="%b%Y"))
str(x2)
#‘zoo’ series from Jun 2012 to Nov 2012
#  Data: num [1:6, 1:5] 2.32 2.46 2.69 2.62 2.61 2.55 2.26 2.21 2.24 2.28 ...
# - attr(*, "dimnames")=List of 2
#  ..$ : NULL
#  ..$ : chr [1:5] "AP" "EU" "LA" "NA." ...
#  Index: Class 'yearmon'  num [1:6] 2012 2012 2013 2013 2013 ...
 plot(x2$NA.,type="l",ylab="M kg/y",xlab="")  #works for me.
#
 plot(x2$`NA`,type='l',ylab='M kg/ y ',xlab='')  #also works (as suggested by HTH)


A.K.





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

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.

______________________________________________
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