[R] error message when using strptime
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Nov 29 01:31:44 CET 2008
You want to convert the datetime to POSIXct, not POSIXlt:
> as.POSIXct("2005-12-20 12:45")
[1] "2005-12-20 12:45:00 EST"
See R News 4/1 for details.
On Fri, Nov 28, 2008 at 7:12 PM, Kara Przeczek <przeczek at unbc.ca> wrote:
> I am new to R and have been struggling with the following problem. I apologize if there is an obvious answer that I have missed.
>
> I bring in a .txt file (pyr_h.txt) that has a DateTime column in the format "yyyy-mm-dd HH:MM"
> When it comes into R, R does not recognize it as a date/time format.
>
> Date Time DateTime N_Wm2 S_Wm2
> 1 4/1/2008 01:00 2008-04-01 01:00 0.0000 0.00000
> 2 4/1/2008 02:00 2008-04-01 02:00 0.0000 0.00000
> 3 4/1/2008 03:00 2008-04-01 03:00 0.0000 0.00000
> 4 4/1/2008 04:00 2008-04-01 04:00 0.0000 0.00000
> 5 4/1/2008 05:00 2008-04-01 05:00 0.0000 0.00000
> 6 4/1/2008 06:00 2008-04-01 06:00 3.1655 2.56175
>
>
> CODE:
> pyr_h = read.table("H:/MastersInfo/data/Figures/Tables/pyr_h.txt",header = T, sep = "\t")
> # convert Date to date format
> pyr_h$Date = as.Date(pyr_h$Date, format = "%m/%d/%Y")
> # convert DateTime to number format
> pyr_h$DateTime = strptime(pyr_h$DateTime, format="%Y-%m-%d %H:%M")
>
> This strptime command works on another .txt file of mine with the exact same date/time stamps, but when I try it for this file, it gives the error message:
>
> Error in `$<-.data.frame`(`*tmp*`, "DateTime", value = list(sec = c(0, :
> replacement has 9 rows, data has 1570
>
> I tried creating a vector:
>
> tmp <-strptime(pyr_h$DateTime, format="%m/%d/%Y %H:%M")
>
> When I queried the length of this vector, it was 9, yet when I print the vector there are 1570 objects.
> Any idea what is going on here? I have tried formatting the DateTime different ways in Excel, recreating the .txt file, changing the strptime format, and rebooting R yet nothing seems to be working.
> Thanks SO much!
> Kara
>
> Kara Przeczek
> M.Sc. Candidate NRES - Environmental Science
> University of Northern B.C.
> 3333 University Way
> Prince George B.C V2N 4Z9
> Phone: (250) 960-5427
> przeczek at unbc.ca
>
>
>
> ______________________________________________
> 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