简体   繁体   English

在R中将小数年和日转换为日期

[英]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? 是否可以将小数年和日转换为R中的实际日期格式?

For example, in the time column in my example data below, 1900.00 corresponds to January of 1900, 1900.08 corresponds to February. 例如,在下面我的示例数据的time列中,1900.00对应于1900年1月,1900.08对应于2月。 dayofyr corresponds to the year day. dayofyr对应于年份。

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 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、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。 ,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.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.25、1900.25、1900.25、1900.25、1900.25、1900.25、1900.25、1900.25、1900.25、1900.25、1900.25 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 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.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.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、0.5.0.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,190。 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 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、190 0.75、190、0.75、190、0.75、190、0.75、190 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.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")) 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,1900.92 :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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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