【发布时间】:2020-01-16 12:46:08
【问题描述】:
我是一个相对较新的 R 用户,这是我在 StackOverflow 上的第一个问题,如果我的问题不清楚或在其他地方明显说明,请致歉。
我有一个大型数据集(7.8 GB,1.37 亿个观测值),我以 ffdf 格式加载到 R 中,因为我的理解是这将允许我操作数据(目的是将其减小到可管理的大小) 不会让我的电脑崩溃。
我的数据集包含六个特征,其中之一是格式为“2012-10-12 00:30:00 BST”的时间戳。由于每次观察(电力读数)都是每隔半小时进行一次,我想根据观察发生当天的 48 个半小时中的哪一个对数据进行分类。因此,作为第一步,我想将日期和时间与时间戳分开。 (之后的目标是每半小时将此时间列编码为 1-48。)
以下代码用于创建新的日期列:
ff1$date <- as.character(as.Date(ff1$DateTime))
但是,我正在努力做同样的事情,并尝试了许多基于可能从其他示例粗略复制的方法。
(1)ff1$time <- as.POSIXct(strptime(as.character(ff1$DateTime),"%T"))
(2)ff1$time <- strptime(ff1$DateTime,"%Y-%m-%d %H:%M:%S")
(3)ff1$time <- sapply(strptime(as.character(ff1$DateTime)," "), "[", 2)
这些都不起作用。上面三行中每一行的错误是:
(1)Error in strptime(as.character(ff1$DateTime), "%T") : invalid 'x' argument
(2)Error in strptime(ff1$DateTime, "%Y-%m-%d %H:%M:%S") : invalid 'x' argument
(3)Error in strptime(as.character(ff1$DateTime), " ") : invalid 'x' argument
这是因为数据是 fdff 格式的吗?还有其他方法吗?
非常感谢!
阿琼
输入:
structure(list(LCLid = structure(c(1L, 1L, 1L, 1L), .Label = "MAC000002", class = "factor"),
stdorToU = structure(c(1L, 1L, 1L, 1L), .Label = "Std", class = "factor"),
DateTime = structure(c(1349998200, 1.35e+09, 1350001800,
1350003600), tzone = "", class = c("POSIXct", "POSIXt")),
KWH.hh..per.half.hour. = structure(c(1L, 1L, 1L, 1L), .Label = " 0 ", class = "factor"),
Acorn = structure(c(1L, 1L, 1L, 1L), .Label = "ACORN-A", class = "factor"),
Acorn_grouped = structure(c(1L, 1L, 1L, 1L), .Label = "Affluent", class = "factor"),
date = structure(c(1L, 2L, 2L, 2L), .Label = c("2012-10-11",
"2012-10-12"), class = "factor")), row.names = c("1", "2",
"3", "4"), class = "data.frame")
相关列的标题:
LCLid DateTime
1 MAC000002 2012-10-12 00:30:00
2 MAC000002 2012-10-12 01:00:00
3 MAC000002 2012-10-12 01:30:00
4 MAC000002 2012-10-12 02:00:00
5 MAC000002 2012-10-12 02:30:00
6 MAC000002 2012-10-12 03:00:00
【问题讨论】: