Convert Julian date to calendar dates within a data frame

前端 未结 3 525
南方客
南方客 2020-12-20 18:18

I have a data frame

> df
Age   year  sex
12    80210  F
13     9123  M

I want to convert the year 80210 as 26june1982. How can I do this t

相关标签:
3条回答
  • 2020-12-20 19:07

    You should substract the origin from the year column.

     as.Date(c(80210,9123)-80210,origin='1982-06-26')
    [1] "1982-06-26" "1787-11-08"
    
    0 讨论(0)
  • 2020-12-20 19:16

    You can convert Julian dates to dates using as.Date and specifying the appropriate origin:

    as.Date(8210, origin=as.Date("1960-01-01"))
    #[1] "1982-06-24"
    

    However, 80210 needs an origin pretty long ago.

    0 讨论(0)
  • 2020-12-20 19:19

    There are some options for doing this job in the R package date.

    See for example on page 4, the function date.mmddyy, which says:

    Given a vector of Julian dates, this returns them in the form “10/11/89”, “28/7/54”, etc.

    Try this code:

    age = c(12,13)
    year = c(8210,9123)
    sex = c("F","M")
    
    df = data.frame(cbind(age,year,sex))
    
    library(date)
    date = date.mmddyy(year, sep = "/")
    
    df2 = transform(df,year=date) #hint provided by jilber
    df2
    
      age     year sex
    1  12  6/24/82   F
    2  13 12/23/84   M
    
    0 讨论(0)
提交回复
热议问题