如何通过POSIXct变量创建昼/夜因素

时间:2019-01-12 20:44:41

标签: r time posixct

我有一个带有POSIXct日期/时间列的数据表。我想创建一个基于POSIXct时间戳记指定“白天”或“晚上”的列。日期在当地时间定义为05:30:00到20:00:00。

我曾尝试使用ifelse语句基于使用strptime创建的“一天中的时间”列来创建新变量,但最终结果很奇怪。

这是一个简单的示例-将其设置为数据表以匹配我的真实数据集。

library(data.table)

SightingTime = c("2017-07-31 09:56:27 UTC", "2017-07-31 10:36:30 UTC", "2017-08-01 00:07:20 UTC","2017-08-01 01:31:00 UTC", "2017-08-01 10:38:23 UTC", "2017-08-01 21:13:06 UTC", "2017-08-02 15:13:30 UTC", "2017-08-02 18:05:28 UTC", "2017-08-02 21:04:08 UTC")
x=data.table(SightingTime)

首先,我从date / time变量中提取一天中的时间-我希望在当地时间显示,因为我会在当地时间指定日出/日落时间。

x$TOD = strftime(x$SightingTime, format="%H:%M:S",tz="America/Halifax")

即使我指定了不同的时区,我也不确定为什么新的TOD变量仍采用UTC。

然后尝试使用ifelse语句创建新变量

x$daynight = with(x,
           ifelse(TOD > 05:30:00 & TOD < 20:00:00, "Day", "Night")) 

由于收到警告消息,并且“白天/夜晚”列中的结果没有意义,因此我显然对此有误。

我希望是这样的。

             SightingTime      TOD daynight
1: 2017-07-31 09:56:27 UTC 06:56:27    Day
2: 2017-07-31 10:36:30 UTC 07:36:30    Day
3: 2017-08-01 00:07:20 UTC 21:07:20    Night
4: 2017-08-01 01:31:00 UTC 22:31:00    Night
5: 2017-08-01 10:38:23 UTC 07:38:23    Day
6: 2017-08-01 21:13:06 UTC 08:13:06    Day
7: 2017-08-02 15:13:30 UTC 12:13:30    Day
8: 2017-08-02 18:05:28 UTC 15:05:28    Day
9: 2017-08-02 21:04:08 UTC 18:04:08    Day

2 个答案:

答案 0 :(得分:1)

数据

library(data.table)

SightingTime_chr = c("2017-07-31 09:56:27 UTC", "2017-07-31 10:36:30 UTC", "2017-08-01 00:07:20 UTC","2017-08-01 01:31:00 UTC", "2017-08-01 10:38:23 UTC", "2017-08-01 21:13:06 UTC", "2017-08-02 15:13:30 UTC", "2017-08-02 18:05:28 UTC", "2017-08-02 21:04:08 UTC")
x = data.table(SightingTime_chr)

代码

在转换为日期时间(例如格式)时,有些事情不太正确:

x$SightingTime = as.POSIXct(x$SightingTime_chr, format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
attributes(x$SightingTime)$tzone <- "America/Halifax"

我首先将字符串转换为POSIXct,然后转换为America / Halifax时区,因为原始矢量似乎在UTC日期时间中(如果我#m错误,则省略第二步)。

x$TOD <- format(x$SightingTime, format="%H%M%S")

x$daynight = with(x, ifelse(TOD > "053000" & TOD < "200000", "Day", "Night")) 

我将一天中的时间转换为一个伪数字值(该值在技术上不正确,但是对于比较目的来说应该足够了。)

x$daynight
[1] "Day"   "Day"   "Night" "Night" "Day"   "Day"   "Day"   "Day"   "Day" 

现在结果显示正确。

替代

this answer中,我们可以得到一个优雅的解决方案,以防万一我们不只是晚上/白天:

nightday <- function(datetime) {
  paste(
    c("Night", "Morning", "Afternoon", "Evening", "Night")[
      cut(as.numeric(format(datetime, "%H%M")), c(0, 530, 1100, 1700 ,2000, 2359))
      ]
  )
}
nightday(x$SightingTime)
[1] "Morning"   "Morning"   "Night"     "Night"     "Morning"   "Evening"   "Afternoon" "Afternoon" "Evening"  

答案 1 :(得分:1)

一种不同的方法可能是首先生成从05:30:00到20:00:00的时间序列(秒),然后比较“ TOD”是否位于该间隔内:

interface ConfigProps {
  // ...
}

interface ConfigState {
 // ...
}

interface TemplateConfiguration<T> {

  Form: React.Component<ConfigProps, ConfigState>;
  Page: React.FunctionComponent<T>; // this one works here

}

const myConfig: TemplateConfiguration  = {
  Page: ({arg1, arg2}) => (<div>...</div>), // and works here
  Form: class extends React.Component<ConfigProps, ConfigState> {
      // ^^^^ --- Type 'typeof Form' is missing the following properties from type 'Component<ConfigProps, ConfigState, any>': context, setState, forceUpdate, render, and 3 more

    render() {
      return (<div>Some text here</div>);
    }
  }
}

class App extends React.Component {

  render() {
    const { Form, Page } = myConfig;
    return (
      <div>
        <Form  /> // JSX element type 'Form' does not have any construct or call signatures
      </div>
    );
  }
}

您可以将其重写为time <- strftime(seq(from=as.POSIXct("1992-5-7 05:30:00", "%Y-%m-%d %H:%M:%S", tz = "America/Halifax"), to=as.POSIXct("1992-5-7 20:00:00", "%Y-%m-%d %H:%M:%S", tz = "America/Halifax"), by= "secs"), format="%H:%M:%S", tz = "America/Halifax") x$SightingTime <- as.POSIXct(x$SightingTime, format = "%Y-%m-%d %H:%M:%S", tz = "UTC") x$TOD <- strftime(x$SightingTime, format="%H:%M:%S", tz="America/Halifax") x$daynight <- with(x, ifelse(TOD %in% time, "Day", "Night")) SightingTime TOD daynight 1: 2017-07-31 09:56:27 06:56:27 Day 2: 2017-07-31 10:36:30 07:36:30 Day 3: 2017-08-01 00:07:20 21:07:20 Night 4: 2017-08-01 01:31:00 22:31:00 Night 5: 2017-08-01 10:38:23 07:38:23 Day 6: 2017-08-01 21:13:06 18:13:06 Day 7: 2017-08-02 15:13:30 12:13:30 Day 8: 2017-08-02 18:05:28 15:05:28 Day 9: 2017-08-02 21:04:08 18:04:08 Day 友好代码:

tidyverse