1

I have ncdf file that can be download here. This dataset contains two variables (TA and DMI). TA value represents the date with unit MONTHS since 1800-01-01 00:00:00. How to convert the TA value into date (as.Date)?

Here my code:

x<-open.ncdf('dmi.cdf')x$
dmi<-get.var.ncdf(x, varid = 'DMI')
ta<-get.var.ncdf(x, varid = 'TA')
date<-as.Date(ta, origin=c('1800-01-01'))

Here I give a result from my code for 12 first row of my dataset. I should be by month not by day.

 > ta[1:12]
 [1] 853.0192 854.0198 855.0205 856.0211
 [5] 857.0218 858.0225 859.0231 860.0238
 [9] 861.0244 862.0251 863.0257 864.0264

 > as.Date(ta[1:12], origin=c('1800-01-01'))
 [1] "1802-05-04" "1802-05-05" "1802-05-06" "1802-05-07"
 [5] "1802-05-08" "1802-05-09" "1802-05-10" "1802-05-11" 
 [9] "1802-05-12" "1802-05-13" "1802-05-14" "1802-05-15"

The result that I expected below (i.e. but the year, I'm not sure it's right 1802)

 [1] "1802-01-01" "1802-02-01" "1802-03-01" "1802-04-01"
 [5] "1802-05-01" "1802-06-01" "1802-07-01" "1802-08-01"
 [9] "1802-09-01" "1802-10-01" "1802-11-01" "1802-12-01"
Eko Susilo
  • 250
  • 2
  • 15
  • 1
    Specify the `format` as well. It should be something like `%Y-%m-%d`. – Roman Luštrik Jul 20 '16 at 08:21
  • It's not about the format, but the date value is not correctly. I give a result from my code for 12 first row of my dataset. I should be by month not by day. – Eko Susilo Jul 20 '16 at 08:54
  • What do you mean "by month not by day"? Please show your desired output. – Abdou Jul 21 '16 at 14:12
  • I added a brief description about the result that I expected on my question – Eko Susilo Jul 22 '16 at 03:49
  • @EkoSusilo the conversion from `integer` to `Date` is something that is based on the integer itself and the origin. Because `ta` is a vector of integers and the origin is `1800-01-01`, you can't just get the dates you desire, sadly. What you can do is: filter the first day of every month from `as.Date(ta, origin=c('1800-01-01'))`. – Abdou Jul 22 '16 at 15:07
  • or: `strftime(as.Date(ta,origin=c('1800-01-01')),format="%Y-%m-01")` – Abdou Jul 22 '16 at 17:29

0 Answers0