Dear all, I would like to convert the first column of a dataframe to a date (original format: year (4 digits) and month (last 2 digits))>str(dat_FF)'data.frame': 1022 obs. of 4 variables: $ date : int 192607 192608 192609 192610 192611 192612 192701 192702 192703 192704 ... $ Rm.Rf: num 2.69 2.52 0 -3.06 2.42 2.66 0 4.29 0.51 0.57 ... $ SMB : num -2.49 -1.25 -1.38 -0.2 -0.34 -0.07 -0.11 0.35 -1.87 0.44 ... $ HML : num -2.91 4.25 0.22 0.71 -0.4 -0.11 4.92 3.17 -2.92 1.33 ... But>dat_FF$date <- as.Date(as.character(dat_FF$date), format="%Y%m")delievers NAs:>str(dat_FF)'data.frame': 1022 obs. of 4 variables: $ date : Date, format: NA NA ... I am very grateful for hints! Thanks in advance! Best, Martin
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Oct-22 20:33 UTC
[R] problem with as.Date
Untested, try this: as.Date(as.character(dat_FF$date), format = "%Y%m") If that doesn't work, let me know. Michael On Oct 22, 2011, at 4:26 PM, "Martin Spindler" <Martin.Spindler at gmx.de> wrote:> Dear all, > > I would like to convert the first column of a dataframe to a date (original > format: year (4 digits) and month (last 2 digits)) > >> str(dat_FF) > 'data.frame': 1022 obs. of 4 variables: > $ date : int 192607 192608 192609 192610 192611 192612 192701 192702 > 192703 192704 ... > $ Rm.Rf: num 2.69 2.52 0 -3.06 2.42 2.66 0 4.29 0.51 0.57 ... > $ SMB : num -2.49 -1.25 -1.38 -0.2 -0.34 -0.07 -0.11 0.35 -1.87 0.44 ... > $ HML : num -2.91 4.25 0.22 0.71 -0.4 -0.11 4.92 3.17 -2.92 1.33 ... > > But > >> dat_FF$date <- as.Date(as.character(dat_FF$date), format="%Y%m") > > delievers NAs: > >> str(dat_FF) > 'data.frame': 1022 obs. of 4 variables: > $ date : Date, format: NA NA ... > > I am very grateful for hints! Thanks in advance! > > Best, > > Martin > > ______________________________________________ > 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.
On 11-10-22 4:26 PM, Martin Spindler wrote:> Dear all, > > I would like to convert the first column of a dataframe to a date (original > format: year (4 digits) and month (last 2 digits)) > >> str(dat_FF) > 'data.frame': 1022 obs. of 4 variables: > $ date : int 192607 192608 192609 192610 192611 192612 192701 192702 > 192703 192704 ... > $ Rm.Rf: num 2.69 2.52 0 -3.06 2.42 2.66 0 4.29 0.51 0.57 ... > $ SMB : num -2.49 -1.25 -1.38 -0.2 -0.34 -0.07 -0.11 0.35 -1.87 0.44 ... > $ HML : num -2.91 4.25 0.22 0.71 -0.4 -0.11 4.92 3.17 -2.92 1.33 ... > > But > >> dat_FF$date<- as.Date(as.character(dat_FF$date), format="%Y%m") > > delievers NAs: > >> str(dat_FF) > 'data.frame': 1022 obs. of 4 variables: > $ date : Date, format: NA NA ... > > I am very grateful for hints! Thanks in advance!I think you need to give a day. For example, I get NA from x <- "192607" as.Date(x, format="%Y%m") but it works with x <- "19260701" as.Date(x, format="%Y%m%d") So I'd suggest pasting 01 (or whichever day you want) to the end of the as.character(dat_FF$date) strings. Duncan Murdoch
On Sat, Oct 22, 2011 at 4:26 PM, Martin Spindler <Martin.Spindler at gmx.de> wrote:> Dear all, > > I would like to convert the first column of a dataframe to a date (original > format: year (4 digits) and month (last 2 digits)) > >>str(dat_FF) > 'data.frame': ? 1022 obs. of ?4 variables: > ?$ date : int ?192607 192608 192609 192610 192611 192612 192701 192702 > 192703 192704 ... > ?$ Rm.Rf: num ?2.69 2.52 0 -3.06 2.42 2.66 0 4.29 0.51 0.57 ... > ?$ SMB ?: num ?-2.49 -1.25 -1.38 -0.2 -0.34 -0.07 -0.11 0.35 -1.87 0.44 ... > ?$ HML ?: num ?-2.91 4.25 0.22 0.71 -0.4 -0.11 4.92 3.17 -2.92 1.33 ... > > But > >>dat_FF$date <- as.Date(as.character(dat_FF$date), format="%Y%m") > > delievers NAs: > >>str(dat_FF) > 'data.frame': ? 1022 obs. of ?4 variables: > ?$ date : Date, format: NA NA ... > > I am very grateful for hints! Thanks in advance! >Here are two approaches: # sample input x <- c(192607, 192608, 192609, 192610, 192611, 192612, 192701, 192702) # 1 - append day as.Date(paste(x, 1), "%Y%m %d") # 2 - yearmon library(zoo) as.Date(as.yearmon(format(x), "%Y%m")) In the last one we use zoo's yearmon which can directly represent year/month without a day and then we convert it to a date. In fact you might just want to leave it as a yearmon and use that directly since it corresponds more closely to what you have:> as.yearmon(format(x), "%Y%m")[1] "Jul 1926" "Aug 1926" "Sep 1926" "Oct 1926" "Nov 1926" "Dec 1926" "Jan 1927" [8] "Feb 1927" See ?yearmon -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com