将文件名转换为年+周的日期将返回charToDate(x)中的错误:字符串不是标准的明确格式

时间:2019-04-05 09:09:03

标签: r time-series as.date

要对栅格堆栈中的1000多个栅格进行时间序列分析,我需要日期。数据几乎是每周在文件的结构中 “ ... 1981036 .... tif” 零分隔年和周 我需要类似“ 1981-36”的

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

(common_py3) PS E:\virtual_env_all\common_py3\Scripts> python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> input_list = [int(x.strip()) for x in input("enter list:").strip()[1:-1].split(",")]
enter list:[2,4,5]
>>> input_list
[2, 4, 5]
>>> type(input_list)
<class 'list'>
>>>

带给我: all_raster

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

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

".../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

但是我需要==“ 1981-35”以便以后绘制时间序列

我尝试过:

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

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

或者我尝试过

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

并得到相同的错误

2 个答案:

答案 0 :(得分:0)

直到有人发布更好的方法,您可以尝试

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

答案 1 :(得分:0)

如果您指定星期一是带有%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将Sunday设置为0来执行相同的操作。