I have a data frame in which one of the columns is date
, but it is as a number. I want to transform it to POSIXct format, but I don't know how.
I thought of using different functions (gsub
, substr
, etc), but none of them allows me to add "-" between numbers at specific positions as far as I know.
How could I do it?
As an example, I have this data frame df1
:
df1<- data.frame(date=c(20160801,20160802,20160803),
var1=c(4,56,76))
> df1
date var1
1 20160801 4
2 20160802 56
3 20160803 76
I want to get this:
> df1
date var1
1 2016-08-01 4
2 2016-08-02 56
3 2016-08-03 76
How do I do it?