【问题标题】:Recall in date POSIXct召回日期 POSIXct
【发布时间】:2013-08-30 14:09:53
【问题描述】:

我找不到 POSIXct 格式问题的解决方案 - 我有每月数据。这是我的代码片段:

Data <- as.POSIXct(as.character(czerwiec$Data), format = "%Y-%m-%d %H:%M:%S")
get.rows <- Data >= as.POSIXct(as.character("2013-06-03 00:00:01")) & Data <= as.POSIXct(as.character("2013-06-09 23:59:59")) 
czerwiec <- czerwiec[get.rows,]
Data <- Data[get.rows]

我选择了 6 月 3 日至 9 日的一个洞周,并想按每个小时估算 X 列 (czerwiec$X) 的总和。如您所见,我可以减少时间,但这样做会很愚蠢,像这样

get.rows <- Data >= as.POSIXct(as.character("2013-06-03 00:00:01")) & 
  Data <= as.POSIXct(as.character("2013-06-03 00:59:59")) 

然后

get.rows <- Data >= as.POSIXct(as.character("2013-06-04 00:00:01")) & 
  Data <= as.POSIXct(as.character("2013-06-04 00:59:59"))

在这个操作结束时,我可以估计这个小时的总和等等。

你有什么想法,我怎么能回忆起每一行,其中有时间像 2013-06-03 到 2013-06-09 和 00:00:01 到 00:59:59??

关于数据框“czerwiec”的一些信息,所以我有三列,其中第一个称为“ID”,第二个称为“价格”,第三个称为“数据”(表示日期)。

感谢帮助:)

【问题讨论】:

  • 请添加代码让我们重现数据框czerwiec
  • Drew Steen,你有足够的信息来帮助我吗???提前谢谢:)
  • 请参阅stackoverflow.com/questions/5963269/… 了解如何制作可重现的示例 - 这将帮助 SO 用户更好地了解您的数据是什么以及您要如何处理这些数据

标签: r posixct


【解决方案1】:

这可能会有所帮助。我使用了 lubridate 包,它并没有真正做你在基础 R 中不能做的任何事情,但它使处理日期变得更加容易

# Set up Data as a string vector
Data <- c("2013-06-01 05:05:05", "2013-06-06 05:05:05", "2013-06-06 08:10:05", "2013-07-07 05:05:05")

require(lubridate)

# Set up the data frame with fake data. This makes a reproducible example
set.seed(4) #For reproducibility, always set the seed when using random numbers

# Create a data frame with Data and price
czerwiec <- data.frame(price=runif(4))

# Use lubridate to turn the Data string into a vector of POSIXctn objects
czerwiec$Data <- ymd_hms(Data) 

# Determine the 'yearday' -i.e. yearday of Jan 1 is 1; yearday of Dec 31 is 365 (or 366 in a leap year)
czerwiec$yday <- yday(czerwiec$Data) 

# in.range is true if the date is in the desired date range
czerwiec$in.range <- czerwiec$yday[czerwiec$yday >= yday(ymd("2013-06-03")) & 
                                     czerwiec$yday yday(ymd("2013-06-09")] 

# Pick out the dates that have the range that you want
selected_dates <- subset(czerwiec, in.range==TRUE)

【讨论】:

    猜你喜欢
    • 2021-09-08
    • 2018-02-04
    • 1970-01-01
    • 2020-08-30
    • 2014-12-04
    • 2013-05-09
    • 2020-11-29
    • 2021-09-04
    相关资源
    最近更新 更多