如何在R中仅从年份和朱利安日(yday)获取日历日期

时间:2017-11-15 23:19:56

标签: r date tidyverse lubridate

我的数据集包含包含year的列,以及包含每年的序数(yday)日的列。我想将此信息转换为日历日期,因此我可以更好地过滤数据集。数据看起来像这样,但是从1980年到2016年,并且每年的每一天都有一个条目:

年yday temp

1980 1 0.5
1980 2 -5.0 1980 3 -3.5
1980 4 1.0
1980 5 -1.0

temps<-structure(list(year = c(1980L, 1980L, 1980L, 1980L, 1980L), yday = 1:5, 
    temp = c(0.5, -5, -3.5, 1, -1)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("year", "yday", "temp"))

我尝试了以下代码,但无法获得正确的日历日期: Convert day of year to date

2 个答案:

答案 0 :(得分:3)


library(lubridate)
library(dplyr)

temps <- tibble::tribble(
  ~year, ~yday, ~temp,
  1980L,    1L,    0.5,
  1980L,    2L,     -5,
  1980L,    3L,   -3.5,
  1980L,    4L,      1,
  1980L,    5L,     -1,
  1980L,    99L,    -1,
  1980L,    50L,    -1
)

temps %>% 
  mutate(date = make_date(year) + yday - 1)

#> # A tibble: 7 x 4
#>    year  yday  temp       date
#>   <int> <int> <dbl>     <date>
#> 1  1980     1   0.5 1980-01-01
#> 2  1980     2  -5.0 1980-01-02
#> 3  1980     3  -3.5 1980-01-03
#> 4  1980     4   1.0 1980-01-04
#> 5  1980     5  -1.0 1980-01-05
#> 6  1980    99  -1.0 1980-04-08
#> 7  1980    50  -1.0 1980-02-19

答案 1 :(得分:2)

您可以从1月1日开始,然后通过添加post_install do |installer| copy_pods_resources_path = "Pods/Target Support Files/Pods-IconTest/Pods-IconTest-resources.sh" string_to_replace = '--compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"' assets_compile_with_app_icon_arguments = '--compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}" --app-icon "${ASSETCATALOG_COMPILER_APPICON_NAME}" --output-partial-info-plist "${BUILD_DIR}/assetcatalog_generated_info.plist"' text = File.read(copy_pods_resources_path) new_contents = text.gsub(string_to_replace, assets_compile_with_app_icon_arguments) File.open(copy_pods_resources_path, "w") {|file| file.puts new_contents } end 值来计算它。

yday