[R] write function to convert to date or delete
William Dunlap
wdunlap at tibco.com
Thu Feb 27 17:42:26 CET 2014
> Error in as.POSIXlt.character(as.character(x), ...) :
> character string is not in a standard unambiguous format.
That error occurs when as.POSIXlt is looking for a format
with which to parse the strings. If you supply a format for the
date then as.POSIXlt will not give this error - it will just return
NA's for the entries that do not match the format.
> as.POSIXlt(c("2013-02-28", "Feb 28, 2013"))
Error in as.POSIXlt.character(c("2013-02-28", "Feb 28, 2013")) :
character string is not in a standard unambiguous format
> as.POSIXlt(c("2013-02-28", "Feb 28, 2013"), format="%Y-%m-%d")
[1] "2013-02-28" NA
> as.POSIXlt(c("2013-02-28", "Feb 28, 2013"), format="%B %d, %Y")
[1] NA "2013-02-28"
If the strings may be in one of several formats, loop through
the formats and decide which to accept.
Bill Dunlap
TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Bill
> Sent: Wednesday, February 26, 2014 10:03 PM
> To: r-help at r-project.org
> Subject: [R] write function to convert to date or delete
>
> I have a dataframe that looks like the below. I want to convert the
> Captured.Time field to a date object. but some of the entries are not
> properly formated and I get a message saying
> Error in as.POSIXlt.character(as.character(x), ...) :
> character string is not in a standard unambiguous format.
>
> So I want to write a function that will convert or delete. I could not
> figure out how to do that and so I tried to write a function that would
> convert or replace with text like noDateHere but that did not work either.
> Can anyone tell me how to accomplish this?
> Here is what I tried:
>
> convertOrOmit=function(dt){tryCatch(as.POSIXct(dt),error=print("noDateHere"))}
>
> X Captured.Time Latitude Longitude Value Unit Location.Name
> 1 12696963 2012-08-07 11:00:51 39.16094 140.4883 45 cpm
> 2 2056198 2013-11-10 03:14:19 32.84428 -117.2240 47 cpm
> 3 727957 2014-01-28 04:47:54 35.80605 139.3789 28 cpm
> 4 2864220 2013-10-22 19:41:53 35.07816 -106.6123 50 cpm
> 5 5787688 2013-06-13 04:13:57 35.83174 136.2027 35 cpm
> 6 6191345 2013-05-28 06:48:34 34.78944 137.9496 32 cpm
> Device.ID MD5Sum Height Surface Radiation
> 1 NA b0465019b46289b82450c39ce1397b98 NA NA
> 2 NA 8fa14a1227d23e6cf286785e8843cc39 NA NA
> 3 NA c72cd7f9cedd59cf6e6892049dfbf9a0 NA NA
> 4 NA aca82e39ff9098e45eea04f661f68dc7 NA NA
> 5 NA cc9394e6dceb91f0e0de97cc2db57e19 NA NA
> 6 NA f18d194a41e1448c7776dbeba8b351af NA NA
> Uploaded.Time Loader.ID
> 1 2012-08-13 19:16:10.18555 10832
> 2 2013-12-05 01:47:24.154971 13958
> 3 2014-01-29 22:55:39.138043 14451
> 4 2013-10-26 13:50:17.629869 13743
> 5 2013-06-16 16:17:21.148239 12930
> 6 2013-06-04 23:31:55.455323 12841
>
> [[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