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]]
I think that you need to supply some data with your question. Use dput() to output your data or a subset of it and paste the output into the email See ?dput for more information. Probably a subset of the data would be sufficient. Something like dput(head(localRaw, 50) should be fine. John Kane Kingston ON Canada> -----Original Message----- > From: yolande.tra at gmail.com > Sent: Tue, 17 Jul 2012 10:48:28 -0400 > To: r-help at r-project.org > 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.____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!
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.
Hello, Print DateTime, and see what's wrong. When you paste(), you use the order ymd HM with space as separator but when you try to convert to POSIXct you pass a wrong format, not the one created by paste(). Solution: as.POSIXct(DAteTime, format="%Y %m %d %H %M") Hope this helps, Rui Barradas Em 17-07-2012 15:48, Yolande Tra escreveu:> 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. >