0

Okay, so here's a sample of what I have in a data frame

months   other_column1   other_column2    ...
01-1912  ...             ...
02-1912  ...             ...
03-1912  ...             ...
...

Now, I'm trying to convert the column "months" to be the index of the time series, so this is what I'm doing at the moment to try and make it a Date object:

dates <- as.Date(df$months, format="%m-%Y")

This goes through with no error messages, but all I get is a vector full of NA instead of the dates. Any ideas? Do you need more information, don't know what to tell more really?

New to R, seems to be quite powerful, so I'm trying to learn some. Thanks.

Roope
  • 4,325
  • 2
  • 25
  • 50

1 Answers1

0

As you said you need last of the month Try this, courtesy https://stackoverflow.com/a/8334531/2747709 from @DirkEddelBuettel

seq(as.Date("YYYY-MM-DD"), length="No. of Months", by="1 month") - 1

let's start from the beginning, creating your data

data.foo <- data.frame(months=c("01-1912","02-1912","03-1912"),col1=rnorm(3),col2=rnorm(3))
#creating dates, note the format
data.foo$dates <-as.Date(paste("01",data.foo$months,sep="-"),"%d-%m-%Y") 
#getting the last date using code above
data.foo$lastday <- seq(as.Date(data.foo$dates)[2],length=nrow(data.foo), by ="1 month") -1
Community
  • 1
  • 1
infominer
  • 1,943
  • 12
  • 17