user5577792
user5577792

Reputation: 45

Convert julian day to mm/dd and add year from another column in dataframe in r

I have a JulianDay column without year (dat$BeginJD) and I have a Year column (dat$Year). I need a single column with mm/dd/yyyy, extracted from both columns (dat$BeginDate).

The data.frame looks like this:

> head(dat); tail(dat)
Year BeginJD BeginDate
 1976       3        NA
 1977      NA        NA
 1978       4        NA
 1979       2        NA
 1980      15        NA
 1981      NA        NA
 2010      15        NA
 2011     343        NA
 2012       7        NA
 2013     354        NA
 2014     360        NA
 2015     337        NA

I want the data.frame to look like this:

> head(dat); tail(dat)
Year BeginJD BeginDate
 1976       3        1/3/1976
 1977      NA        NA
 1978       4        1/4/1978
 1979       2        1/2/1979
 1980      15        1/15/1980
 1981      NA        NA
 2010      15        1/15/2010
 2011     343        12/9/2011
 2012       7        1/7/2012
 2013     354        12/20/2013
 2014     360        12/26/2014
 2015     337        12/3/2015

I've tried code using lubridate and chron packages with no luck.

Using R 3.2.1 and RStudio 0.99.467

Upvotes: 3

Views: 590

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269905

One can add days to a "Date" class or "chron" variable so this works -- use format(as.Date(paste0(Year, "-1-1")) + BeginJD - 1), "%m/%d/%Y") to format it if you want the result as a character string rather than a "Date" class object; however, normally one stores "Date" class objects, not formatted strings.

transform(ser, BeginDate = as.Date(paste0(Year, "-1-1")) + BeginJD - 1)

giving:

   Year BeginJD  BeginDate
1  1976       3 1976-01-03
2  1977      NA       <NA>
3  1978       4 1978-01-04
4  1979       2 1979-01-02
5  1980      15 1980-01-15
6  1981      NA       <NA>
7  2010      15 2010-01-15
8  2011     343 2011-12-09
9  2012       7 2012-01-07
10 2013     354 2013-12-20
11 2014     360 2014-12-26
12 2015     337 2015-12-03

as does this:

library(chron)
transform(ser, BeginDate = as.chron(paste0(Year, "-1-1")) + BeginJD - 1)

giving:

   Year BeginJD BeginDate
1  1976       3  01/03/76
2  1977      NA      <NA>
3  1978       4  01/04/78
4  1979       2  01/02/79
5  1980      15  01/15/80
6  1981      NA      <NA>
7  2010      15  01/15/10
8  2011     343  12/09/11
9  2012       7  01/07/12
10 2013     354  12/20/13
11 2014     360  12/26/14
12 2015     337  12/03/15

Note: Here is the ser that was used in reproducible form. (Please be sure that in the future your questions provide the inputs reproducibly.)

Lines <- "Year BeginJD BeginDate
 1976       3        NA
 1977      NA        NA
 1978       4        NA
 1979       2        NA
 1980      15        NA
 1981      NA        NA
 2010      15        NA
 2011     343        NA
 2012       7        NA
 2013     354        NA
 2014     360        NA
 2015     337        NA"

ser <- read.table(text = Lines, header = TRUE)

Upvotes: 4

Related Questions