[R] POSIXct conversion stops part way through a df column

David Winsemius dwinsemius at comcast.net
Sat Mar 20 22:39:44 CET 2010


On Mar 20, 2010, at 5:08 PM, James Rome wrote:

> I have a date/time imported from Excel in my dataframe oooi (with
> several hundred thousand rows), for example the input data near row  
> 3100 is
> ActualOnLocal
> 11/12/2008 21:35
> 11/12/2008 22:03
> 11/12/2008 22:12
> 11/12/2008 22:38
> 11/12/2008 23:16
> 11/12/2008 23:23
> 11/13/2008 7:00
> 11/13/2008 7:03
> 11/13/2008 7:05
> 11/13/2008 7:11
>
>
> I want to convert the column to POSIXct
> oooi$dpt <- as.POSIXct(oooi$ActualOnLocal, tz="", format="%m/%d/%Y  
> %H:%M")
> It works for the first 3099 rows. But after that all the rows come out
> as NA:
>> oooi$dpt
> .....
> [3089] "0011-12-20 EST" "0011-12-20 EST" "0011-12-20 EST"  
> "0011-12-20 EST"
> [3093] "0011-12-20 EST" "0011-12-20 EST" "0011-12-20 EST"  
> "0011-12-20 EST"
> [3097] "0011-12-20 EST" "0011-12-20 EST" "0011-12-20 EST" NA
> [3101] NA               NA               NA               NA
> [3105] NA               NA               NA               NA
> ....
> What can be causing this? I see nothing strange in the data in the  
> dataframe
>> oooi$ActualOnLocal[3090:3120]
> [1] 11/12/2008 20:17 11/12/2008 20:24 11/12/2008 20:36 11/12/2008  
> 20:50
> [5] 11/12/2008 21:35 11/12/2008 22:03 11/12/2008 22:12 11/12/2008  
> 22:38
> [9] 11/12/2008 23:16 11/12/2008 23:23 11/13/2008 7:00  11/13/2008 7:03
> [13] 11/13/2008 7:05  11/13/2008 7:11  11/13/2008 7:12  11/13/2008  
> 7:21
> [17] 11/13/2008 7:23  11/13/2008 7:23  11/13/2008 7:25  11/13/2008  
> 7:27
> [21] 11/13/2008 7:30  11/13/2008 7:32  11/13/2008 7:33  11/13/2008  
> 7:34
> [25] 11/13/2008 7:34  11/13/2008 7:35  11/13/2008 7:37  11/13/2008  
> 7:38
> [29] 11/13/2008 7:39  11/13/2008 7:41  11/13/2008 7:45
> 94093 Levels: 1/1/2009 10:00 1/1/2009 10:01 1/1/2009 10:04 ...  
> 9/9/2009 9:59

There is your big hint right there. You read them in as factors!  
as.POSIXct is probably working on the integers rather than on the  
levels, which is what you are seeing when you display them.

Use strngsAsFactors=FALSE in your read operation.

>
> What is causing the conversion to suddenly fail?
>
> Thanks,
> Jim
>
>
> 	[[alternative HTML version deleted]]



David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list