【问题标题】:Separating time and date values from a timestamp/ DateTime column in an ffdf从 ffdf 中的时间戳/日期时间列中分离时间和日期值
【发布时间】: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

【问题讨论】:

    标签: r datetime ff


    【解决方案1】:

    如果您经常使用日期和时间,lubridate 可能会有所帮助。这里我使用ymd_hms()来转换year-month-day hour-minute-s第二种格式转换为实际的日期时间。然后使用格式。

    这与其他解决方案没有本质区别,只是转换回日期时间的不同方式。

    代码:

    library(lubridate)
    
    ff1$time <- format(ymd_hms(ff1$DateTime), format = "%H:%M:%S")
    

    结果:

    > ff1
          LCLid stdorToU            DateTime KWH.hh..per.half.hour.   Acorn Acorn_grouped       date     time
    1 MAC000002      Std 2012-10-11 19:30:00                     0  ACORN-A      Affluent 2012-10-11 19:30:00
    2 MAC000002      Std 2012-10-11 20:00:00                     0  ACORN-A      Affluent 2012-10-12 20:00:00
    3 MAC000002      Std 2012-10-11 20:30:00                     0  ACORN-A      Affluent 2012-10-12 20:30:00
    4 MAC000002      Std 2012-10-11 21:00:00                     0  ACORN-A      Affluent 2012-10-12 21:00:00
    

    【讨论】:

    • 感谢亚当。我在这里得到的错误是Error in `[[&lt;-.ffdf`(`*tmp*`, i, value = character(0)) : assigned value must be ff
    【解决方案2】:

    您尝试的代码出错可能是因为"DateTime 列不属于"POSIXt""POSIXct" 类。因此,首先强制转换为日期/时间类,然后仅提取时间。

    ff1$DateTime <- as.POSIXct(ff1$DateTime)
    format(ff1$DateTime, format = "%T")
    #[1] "00:30:00"
    

    编辑。

    如果上面给出错误尝试

    ff1$DateTime <- as.POSIXct(as.character(ff1$DateTime))
    format(ff1$DateTime, format = "%T")
    

    数据。

    ff1 <- data.frame(DateTime = "2012-10-12 00:30:00 BST")
    

    【讨论】:

    • 谢谢瑞!我收到错误Error in as.POSIXct.default(ff1$DateTime) : do not know how to convert 'ff1$DateTime' to class “POSIXct”
    • format(as.POSIXct(as.character(ff1$DateTime)), format="%T"),可能。
    • 我认为这已经解决了! ff1$Time &lt;- format(ff1$DateTime, format = "%T")
    【解决方案3】:

    你可以使用strsplit

    sapply(strsplit(as.character(dat$x), " "), `[`, 1)
    # [1] "2012-10-12" "2012-10-12" "2012-10-12" "2012-10-12" "2012-10-12"
    sapply(strsplit(as.character(dat$x), " "), `[`, 2)
    # [1] "00:30:00" "00:30:00" "00:30:00" "00:30:00" "00:30:00"
    

    数据:

    x <- "2012-10-12 00:30:00 BST"
    dat <- data.frame(x=replicate(5, x))
    

    【讨论】:

    • 感谢 jay 的快速回复!我按照建议尝试了 strsplit 并得到错误“strsplit(ff1$DateTime,“”)中的错误:非字符参数”
    • 然后我还尝试创建一个数据框:t1 &lt;- data.frame(x=replicate(5, ff1$DateTime), stringsAsFactors=F) 我的第一个问题是这只会占用一个子集,对吧?我想我想编辑整个数据集,这样我就可以按时间对观察结果进行分类。
    • 但我也收到了错误Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ‘c("ff_vector", "ff")’ to a data.frame
    • @arj 在数据周围包裹as.character(.) 应该可以工作,请参阅更新。
    • @arj 最好始终使用dput 提供数据。请阅读:stackoverflow.com/questions/5963269/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2022-01-14
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    相关资源
    最近更新 更多