strange problem with strptime and date variable

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

strange problem with strptime and date variable

Edwin Sun
Hello all,

I am manipulating a large database with 70,000 records. "strptime" generates a date variable but R treats some of the values as NA. I attach a simple example below.

I have spent hours on this problem. Any hint would be greatly appreciated.

Many thanks,

Edwin Sun

# =======start of sample code =============

> x <- c("2005-04-02 19:03:00", "2005-04-03 02:00:00", "2005-04-03 14:25:00")
 
> y <- strptime(x, format="%Y-%m-%d %H:%M:%S")
 
> x; y; str(x); str(y)
[1] "2005-04-02 19:03:00" "2005-04-03 02:00:00" "2005-04-03 14:25:00"
[1] "2005-04-02 19:03:00" "2005-04-03 02:00:00" "2005-04-03 14:25:00"
 chr [1:3] "2005-04-02 19:03:00" "2005-04-03 02:00:00" ...
 POSIXlt[1:3], format: "2005-04-02 19:03:00" "2005-04-03 02:00:00" ...

> is.na(y)
[1] FALSE  TRUE FALSE

> difftime(y[2], y[1], units="mins")
Time difference of NA mins
 
> difftime(y[3], y[1], units="mins")
Time difference of 1102 mins
 
> sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grDevices datasets  splines   graphics  stats     tcltk     utils    
[8] methods   base    

other attached packages:
[1] svSocket_0.9-51 TinnR_1.0.3     R2HTML_2.2      Hmisc_3.8-3    
[5] survival_2.36-9

loaded via a namespace (and not attached):
[1] cluster_1.14.0  grid_2.13.1     lattice_0.19-30 svMisc_0.9-61  
[5] tools_2.13.1  

# My computer is 64 bit / Microsoft Window 7; R 2.13.1 (32 bit)

# ======= end of sample code ====================


Reply | Threaded
Open this post in threaded view
|

Re: strange problem with strptime and date variable

David Winsemius

On Jul 18, 2011, at 11:21 PM, Edwin Sun wrote:

> Hello all,
>
> I am manipulating a large database with 70,000 records. "strptime"  
> generates
> a date variable but R treats some of the values as NA. I attach a  
> simple
> example below.

Cannot duplicate but I'm betting on three words:

daylight savings time

--
David.

>
> I have spent hours on this problem. Any hint would be greatly  
> appreciated.
>
> Many thanks,
>
> Edwin Sun
>
> # =======start of sample code =============
>
>> x <- c("2005-04-02 19:03:00", "2005-04-03 02:00:00", "2005-04-03
>> 14:25:00")
>
>> y <- strptime(x, format="%Y-%m-%d %H:%M:%S")
>
>> x; y; str(x); str(y)
> [1] "2005-04-02 19:03:00" "2005-04-03 02:00:00" "2005-04-03 14:25:00"
> [1] "2005-04-02 19:03:00" "2005-04-03 02:00:00" "2005-04-03 14:25:00"
> chr [1:3] "2005-04-02 19:03:00" "2005-04-03 02:00:00" ...
> POSIXlt[1:3], format: "2005-04-02 19:03:00" "2005-04-03 02:00:00" ...
>
>> is.na(y)
> [1] FALSE  TRUE FALSE
>
>> difftime(y[2], y[1], units="mins")
> Time difference of NA mins
>
>> difftime(y[3], y[1], units="mins")
> Time difference of 1102 mins
>
>> sessionInfo()
> R version 2.13.1 (2011-07-08)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] grDevices datasets  splines   graphics  stats     tcltk     utils
> [8] methods   base
>
> other attached packages:
> [1] svSocket_0.9-51 TinnR_1.0.3     R2HTML_2.2      Hmisc_3.8-3
> [5] survival_2.36-9
>
> loaded via a namespace (and not attached):
> [1] cluster_1.14.0  grid_2.13.1     lattice_0.19-30 svMisc_0.9-61
> [5] tools_2.13.1
>
> # My computer is 64 bit / Microsoft Window 7; R 2.13.1 (32 bit)
>
> # ======= end of sample code ====================
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/strange-problem-with-strptime-and-date-variable-tp3677178p3677178.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: strange problem with strptime and date variable

Edwin Sun
In reply to this post by Edwin Sun
David - Thanks very much. That is a great hint. I believe that is the solution.

Edwin Sun