[R] Date from text

Marc Schwartz
2020-05-15T11:44:02Z

Hi,

For the usual R text to date conversions, you need a complete date. Since you
are missing the day of the month in your source text, you would need to impute
that part before making the conversion.

Also, since you don't appear to need to worry about time of day, just use
as.Date(), instead of as.POSIXct().

In this case, use ?sub to add the day of the month to the source text, for which
I will impute the 15th, and then make the conversion:

## Single value for now
x <- "11-1993"

> sub("-", "-15-", x)
[1] "11-15-1993"

> as.Date(sub("-", "-15-", x), format =
"%m-%d-%Y")
[1] "1993-11-15"


Regards,

Marc Schwartz



> On May 15, 2020, at 5:38 AM, Poizot Emmanuel <emmanuel.poizot at
lecnam.net> wrote:
>
> Dear all,
>
> I've a data frame with a column "Date":
>
> [1] 11-1993 11-1993 11-1993 11-1993 11-1993 11-1993 11-1996 11-1996 11-1996
> [10] 11-1996 11-1996 11-1996 02-1998 02-1998 02-1998 02-1998 02-1998
02-1998
> [19] 11-1998 11-1998 11-1998 11-1998 11-1998 11-1998 10-2001 10-2001
10-2001
> [28] 10-2001 10-2001 10-2001 02-2003 02-2003 02-2003 02-2003 02-2003
02-2003
> [37] 11-2004 11-2004 11-2004 11-2004 11-2004 11-2004 11-2005 11-2005
11-2005
> [46] 11-2005 11-2005 11-2005 11-2007 11-2007 11-2007 11-2007 11-2007
11-2007
> [55] 10-2008 10-2008 10-2008 10-2008 10-2008 10-2008 03-2009 03-2009
03-2009
> [64] 03-2009 03-2009 03-2009 10-2012 10-2012 10-2012 10-2012 10-2012
10-2012
> [73] 12-2017 12-2017 12-2017 12-2017 12-2017 12-2017 12-2018 12-2018
12-2018
> [82] 12-2018 12-2018 12-2018
>
> I want to convert that into real dates:
> as.POSIXct(Date, format="%m-%Y") always return "NA"
values.
> Where am I wrong ?
>
> regards