【问题标题】:Converting filenames to date in year + weeks returns Error in charToDate (x): character string is not in a standard unambiguous format将文件名转换为年 + 周中的日期返回 charToDate (x) 中的错误:字符串不是标准的明确格式
【发布时间】:2019-08-27 03:37:22
【问题描述】:

对于栅格堆栈中超过 1000 个栅格的时间序列分析,我需要日期。文件结构中的数据几乎是每周一次 “... 1981036 .... tif” 零分隔年份和星期 我需要类似:“1981-36”

但总是得到错误 charToDate (x) 中的错误:字符串不是标准的明确格式

library(sp)
library(lubridate)
library(raster)
library(Zoo)

raster_path <- ".../AVHRR_All"
all_raster <- list.files(raster_path,full.names = TRUE,pattern = ".tif$")
all_raster

带给我: all_raster

".../VHP.G04.C07.NC.P1981036.SM.SMN.Andes.tif"
".../VHP.G04.C07.NC.P1981037.SM.SMN.Andes.tif"
".../VHP.G04.C07.NC.P1981038.SM.SMN.Andes.tif"
…

为了获取年份和相关的星期,我使用了以下代码:

timeline <- data.frame(
  year= as.numeric(substr(basename(all_raster), start = 17, stop = 17+3)),
  week= as.numeric(substr(basename(all_raster), 21, 21+2))
)
timeline

带给我: 时间线

     year week
1    1981   35
2    1981   36
3    1981   37
4    1981   38
…

但我需要像 = "1981-35" 这样的东西才能在以后绘制我的时间序列

我试过了:

timeline$week <- as.Date(paste0(timeline$year, "%Y")) + week(timeline$week -1, "%U")

并得到错误:charToDate(x) 中的错误:字符串不是标准的明确格式

或者我试过了

fileDates <- as.POSIXct(substr((all_raster),17,23), format="%y0%U")

得到同样的错误

【问题讨论】:

标签: r time-series as.date


【解决方案1】:

在有人发布更好的方法之前,您可以尝试:

x <- c(".../VHP.G04.C07.NC.P1981036.SM.SMN.Andes.tif", ".../VHP.G04.C07.NC.P1981037.SM.SMN.Andes.tif",
       ".../VHP.G04.C07.NC.P1981038.SM.SMN.Andes.tif")

xx <- substr(x, 21, 27)


library(lubridate)


dates <- strsplit(xx,"0")
dates <- sapply(dates,function(x) {
  year_week <- unlist(x)
  year <- year_week[1]
  week <- year_week[2]
  start_date <- as.Date(paste0(year,'-01-01'))
  date <- start_date+weeks(week)
  #note here: OP asked for beginning of week.  
  #There's some ambiguity here, the above is end-of-week; 
  #uncommment here for beginning of week, just subtracted 6 days.  
  #I think this might yield inconsistent results, especially year-boundaries
  #hence suggestion to use end of week.  See below for possible solution
  #date <- start_date+weeks(week)-days(6)

  return (as.character(date))
})


newdates <- as.POSIXct(dates)
format(newdates, "%Y-%W")

感谢@Soren 在此处发布此问题:Get the month from the week of the year

【讨论】:

  • 非常感谢 Andrei,如果我调整并运行您的代码,我会在“newdates”中得到很多“NA”
  • NA 如果在用于分隔的零之后仍然有一个零,则将显示 01,对于第 1 周看到的内容。示例:“1982001”
【解决方案2】:

如果您使用%u 指定星期一是工作日 1,则可以这样做:

w <- c(35,36,37,38)
y <- c(1981,1981,1981,1981)
s <- c(1,1,1,1)
df <- data.frame(y,w,s)
df$d <- paste(as.character(df$y), as.character(df$w),as.character(df$s), sep=".")
df$date <- as.Date(df$d, "%Y.%U.%u")

# So here we have variable date as date if you need that for later. 
class(df$date)
#[1] "Date"

# If you want it to look like Y-W, you can do the final formatting:
df$date <- format(df$date, "%Y-%U")

#     y  w s         d    date
# 1 1981 35 1 1981.35.1 1981-35
# 2 1981 36 1 1981.36.1 1981-36
# 3 1981 37 1 1981.37.1 1981-37
# 4 1981 38 1 1981.38.1 1981-38

# NB: though it looks correct, the resulting df$date is actually a character: 
class(df$date)
#[1] "character"

或者,您也可以使用%w 将星期日设置为 0。

【讨论】:

  • 这段代码运行良好 这段代码运行良好,直到将 as.character 转换为 as.date。在最后一步中,“日期”保留了“字符”类而不是“日期”类
  • 如果您需要日期变量作为日期,您可以在最终格式化之前保留日期变量(我将其添加到答案中)
  • 我已经添加并重写了你的代码,对我来说这个类仍然是字符而不是日期
  • 嗯,在我的设置类中,format 之前是“日期”,之后是“字符”。您在执行脚本期间是否收到任何错误或警告??
  • 因为我需要日期格式 %Y-% U,我无法避免以这种格式存储它。我没有收到错误消息。他将其保存为一个字符,然后我将其转换为日期,然后进行格式设置,它又是一个字符...
猜你喜欢
  • 2015-01-27
  • 2021-12-30
  • 2021-01-20
  • 2020-03-03
  • 2018-11-04
  • 1970-01-01
  • 2013-08-29
  • 2020-07-02
  • 2013-01-23
相关资源
最近更新 更多