Home > Software engineering >  Transform year/day colnames into date in r
Transform year/day colnames into date in r

Time:06-29

I have the following columns' names of dataframe in year/day of the year format

colnames(ndvi.df)
 [1] "gid"     "xcoord"  "ycoord"  "col"     "row"     "gwno"    "country" "km2"    
 [9] "2000049" "2000065" "2000081" "2000097" "2000113" "2000129" "2000145" "2000161"
[17] "2000177" "2000193" "2000209" "2000225" "2000241" "2000257" "2000273" "2000289"
[25] "2000305" "2000321" "2000337" "2000353"

How can I transform them into the corresponding year-month_day format please ?

Example: 2000049 = 18/02/2000

CodePudding user response:

You can use lubridate

library(lubridate)
cols = colnames(ndvi.df)
ymd(parse_date_time(cols[9:length(cols)], orders="yj"))

Output:

 [1] "2000-02-18" "2000-03-05" "2000-03-21" "2000-04-06" "2000-04-22" "2000-05-08" "2000-05-24" "2000-06-09"
 [9] "2000-06-25" "2000-07-11" "2000-07-27" "2000-08-12" "2000-08-28" "2000-09-13" "2000-09-29" "2000-10-15"
[17] "2000-10-31" "2000-11-16" "2000-12-02" "2000-12-18"
  • Related