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
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
On Fri, 15 May 2020, Poizot Emmanuel writes:> 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 ? > > regardsIf you really want a date, I'd suggest 'as.Date'. The help for ?as.Date says: "If the date string does not specify the date completely, the returned answer may be system-specific." So perhaps try something like as.Date(paste0("01-", "11-1993"), format = "%d-%m-%Y") ## [1] "1993-11-01" Or look at 'yearmon' in package 'zoo': library("zoo") as.yearmon("11-1993", format = "%m-%Y") ## [1] "Nov 1993" -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net