简体   繁体   中英

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.2 5, 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, 19 00.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, 190 0.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"))

使用floor删除小数部分,转换为带"-01-01"字符串,将其转换为"Date"类,然后加上减1的天数。不使用任何包。

transform(myData, date = as.Date(paste0(floor(time), "-01-01")) + dayofyr - 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM