SoilSciGuy - 1 year ago 61
R Question

# Convert fractional year and day of year to date in R

Is it possible to convert a fractional year and day of year to an actual date format in R?

For example, in the

`time`
column in my example data below, 1900.00 corresponds to January of 1900, 1900.08 corresponds to February.
`dayofyr`
corresponds to the year day.

myData <- structure(list(time = c(1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08,
1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08,
1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08,
1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08,
1900.08, 1900.08, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17,
1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17,
1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17,
1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17,
1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.25, 1900.25,
1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25,
1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25,
1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25,
1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25,
1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33,
1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33,
1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33,
1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33,
1900.33, 1900.33, 1900.33, 1900.42, 1900.42, 1900.42, 1900.42,
1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42,
1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42,
1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42,
1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.5, 1900.5,
1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5,
1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5,
1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5,
1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.58, 1900.58, 1900.58,
1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58,
1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58,
1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58,
1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58,
1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67,
1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67,
1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67,
1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67,
1900.67, 1900.67, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75,
1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75,
1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75,
1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75,
1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.83, 1900.83,
1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83,
1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83,
1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83,
1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83,
1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92,
1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92,
1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92,
1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92,
1900.92, 1900.92, 1900.92), dayofyr = 1:366), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -366L), .Names = c("time",
"dayofyr"))

Use `floor` to remove the decimal part, convert to a character string appending `"-1-1"`, convert that to `"Date"` class and add the number of days minus 1. No packages are used.
``````transform(myData, date = as.Date(paste(floor(time), 1, 1, sep = "-")) + dayofyr - 1)