[R] NA instead of time stamp

arun smartpink111 at yahoo.com
Tue Jul 17 19:27:41 CEST 2012


Hello,

I think there is some mistake in the format.

Try this:


dat1<-data.frame(Year=rep(2009,2),Month=rep(10,2),Day=rep(5,2),hour=rep(0,2),minute=c(0,15),second=c(11.288,11.258))

###Your code

 DateTime<-with(dat1,paste(Year,Month,Day,hour,minute))

DateTime
#[1] "2009 10 5 0 0"  "2009 10 5 0 15"

 DateTime1<-as.POSIXct(DateTime,format="%m/%d/Y %H:%M")
 DateTime1
#[1] NA NA


######Corrected one

DateTime<-with(dat1,paste(Year,Month,Day,hour,minute,sep="/"))
 DateTime1<-as.POSIXct(DateTime,format="%Y/%m/%d/%H/%M")

 DateTime1
#[1] "2009-10-05 00:00:00 EDT" "2009-10-05 00:15:00 EDT"
 

A.K.




----- Original Message -----
From: Yolande Tra <yolande.tra at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, July 17, 2012 10:48 AM
Subject: [R] NA instead of time stamp

I was wondering why I get <NA> instead of the timestamp in the following.
Thanks.

> dataDir <- file.path(wd)
> localRaw <- read.csv(file.path(dataDir,"LOCAL.csv"), as.is=T,stringsAsFactors
= FALSE)
> localRaw[1:2,]
  Year Month Day hour minute second Temp1mab Temp7mab Temp14mab Salinity1mab
1 2009    10   5    0      0      0   11.288   13.675    13.743       33.513
2 2009    10   5    0     15      0   11.258   13.684    13.716       33.514
  O21mab waveht wavedir waveperiod depth mab along across
1      0 54.926   1.600        340  9.09   .    NA     NA
2      0 55.574   1.525        340  9.09   .    NA     NA
> DateTime<-with(localRaw,paste(Year,Month,Day,hour,minute))
> DateTime<-as.POSIXct(DateTime, format="%m/%d/%Y %H:%M")
> localCond_2009<-xts(localRaw ,order.by=DateTime)
> localCond_2009[1:2,]
     Year   Month Day  hour minute second Temp1mab Temp7mab Temp14mab
<NA> "2009" "10"  " 5" " 0" " 0"   "0"    "11.288" "13.675" "13.743"
<NA> "2009" "10"  " 5" " 0" "15"   "0"    "11.258" "13.684" "13.716"
     Salinity1mab O21mab waveht   wavedir waveperiod depth    mab along
across
<NA> "33.513"     "0"    "54.926" "1.600" "340.0"    " 9.090" "." NA
NA
<NA> "33.514"     "0"    "55.574" "1.525" "340.0"    " 9.090" "." NA
NA

    [[alternative HTML version deleted]]

______________________________________________
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