【问题标题】:as.Date character string with month-day dataas.Date 字符串与月日数据
【发布时间】:2020-04-14 05:51:40
【问题描述】:

您好,我正在尝试确定我的数据点是否出现在建议的工作季节。我有这个功能:

### Get whether or not sighting occurred during the proposed work period
getWork <-function(w) {
    startWork <- as.Date("07-15", format = "%m-%d") #Start of stated work period
    endWork <- as.Date("02-15", format = "%m-%d") #End of stated work period

  ifelse (w >=startWork & w <= endWork, 1, 0)
  }

#convert SightDate to Month-Day
sightingsData$SightMonthDay <- format(sightingsData$SightDate, format="%m-%d")
sightingsData$workPeriod <- getWork(sightingsData$SightMonthDay) #Get Work T/F values for each date

该函数的代码运行顺利,没有错误,但我得到一个错误的输出,如下:

Error in charToDate(x) : 
  character string is not in a standard unambiguous format 
6. stop("character string is not in a standard unambiguous format") 
5. charToDate(x) 
4. as.Date.character(e1) 
3. as.Date(e1) 
2. `>=.Date`(w, startWork) 
1. getWork(sightingsData$SightMonthDay)

但是,如果我更改函数的语法以匹配创建 'SightMonthDay' 列的语法,(i.e. startwork &lt;- as.Date.... instead of startwork &lt;- format....)我会得到一个输出,但所有的 'getWork' 值显然都在 -1 和 0 之间,但是所有的观察结果被认为是 0 [假],在超过 23500 个观察结果中是不正确的。这是说明我的意思的代码:

getWork <-function(w) {
    startWork <- format("07-15", format = "%m-%d") #Start of stated work period
    endWork <- format("02-15", format = "%m-%d") #End of stated work period
    +(w >=startWork & w <= endWork)
}

#convert SightDate to Month-Day
sightingsData$SightMonthDay <- format(sightingsData$SightDate, format="%m-%d")
sightingsData$workPeriod <- getWork(sightingsData$SightMonthDay) #Get Work T/F values for each date

这里有一个小截图来说明我的意思......

这是一个示例数据集:

       SightDate SightMonthDay workPeriod
18163 2017-11-15         11-15          0
17120 2017-10-23         10-23          0

> dput(droplevels(SampleData[1:20, ]))
structure(list(SightDate = structure(c(17485, 17462, 17469, 17417, 
17871, 17294, 16712, 15069, 16576, 17416, 17515, 16407, 16399, 
17298, 17164, 17244, 17464, 15628, 16388, 16598), class = "Date"), 
    SightMonthDay = c("11-15", "10-23", "10-30", "09-08", "12-06", 
    "05-08", "10-04", "04-05", "05-21", "09-07", "12-15", "12-03", 
    "11-25", "05-12", "12-29", "03-19", "10-25", "10-15", "11-14", 
    "06-12"), workPeriod = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(18163L, 
17120L, 17437L, 15597L, 23402L, 14359L, 9874L, 2902L, 8910L, 
15585L, 19641L, 7769L, 7631L, 14399L, 13163L, 13447L, 17297L, 
4511L, 7405L, 9113L), class = "data.frame")

感谢您的宝贵时间和帮助!

【问题讨论】:

    标签: r date dplyr lubridate


    【解决方案1】:

    很可能,您的startWorkendWork 已被替换。

    getWork <-function(w) {
       endWork <- as.Date("07-15", format = "%m-%d") #Start of stated work period
       startWork <- as.Date("02-15", format = "%m-%d") #End of stated work period
       +(w >=startWork & w <= endWork)
    }
    

    并将其应用于SightMonthDay

    SampleData$new_date <- as.Date(format(SampleData$SightDate, "%m-%d"), "%m-%d")
    getWork(SampleData$new_date)
    
    #[1] 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 0 0 1
    

    【讨论】:

    • 谢谢!但是,这对我来说不起作用,至少对于 SightMonthDay 数据格式不起作用。在包含您的反馈后,我更新了我的帖子以显示我现在的位置。感谢您的所有时间和帮助!
    • @SalmaAbdel-Raheem 您需要获取同一年的日期。查看更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2021-10-13
    相关资源
    最近更新 更多