r - 如何在 ff 或 ffbase 中将因子向量转换为 POSIXct

标签 r posixct ff ffbase

在使用 read.csv.ffdf 读取大型数据集后,其中一列是时间。例如 2014-10-18 00:01:02,该列中有 100 万行。该列是一个因素。如何将其转换为 ff 支持的 POSIXct?只需使用 as.POSIXct() 即可将值转换为 NA

或者当我一开始读入数据集时,我可以指定该列为POSIXct吗?

我的目标是获取月份和日期(甚至小时)。所以我对转换为 POSIXct 以外的解决方案持开放态度。

例如,我们有 9 x 2 的表格,

test <- read.csv.ffdf(file="test.csv", header=T, first.rows=-1)

两列是ID(数字类)和时间(因子类)

这里是输出

structure(list(virtual = structure(list(VirtualVmode = c("integer", 
"integer"), AsIs = c(FALSE, FALSE), VirtualIsMatrix = c(FALSE, 
FALSE), PhysicalIsMatrix = c(FALSE, FALSE), PhysicalElementNo = 1:2, 
    PhysicalFirstCol = c(1L, 1L), PhysicalLastCol = c(1L, 1L)), .Names = c("VirtualVmode", 
"AsIs", "VirtualIsMatrix", "PhysicalIsMatrix", "PhysicalElementNo", 
"PhysicalFirstCol", "PhysicalLastCol"), row.names = c("ID", "time"
), class = "data.frame", Dim = c(9L, 2L), Dimorder = 1:2), physical = structure(list(
    ID = structure(list(), physical = <pointer: 0x000000000821ab20>, virtual = structure(list(), Length = 9L, Symmetric = FALSE), class = c("ff_vector", 
    "ff")), time = structure(list(), physical = <pointer: 0x000000000821abb0>, virtual = structure(list(), Length = 9L, Symmetric = FALSE, Levels = c("10/17/2003 0:01", 
    "12/5/1999 0:02", "2/1/2000 0:01", "3/23/1998 0:01", "3/24/2013 0:00", 
    "5/29/2004 0:00", "5/9/1985 0:01", "6/14/2010 0:01", "6/25/2008 0:02"
    ), ramclass = "factor"), class = c("ff_vector", "ff"))), .Names = c("ID", 
"time")), row.names = NULL), .Names = c("virtual", "physical", 
"row.names"), class = "ffdf")

最佳答案

您可以使用来自包 ffbase 的玩具示例,如下所示。最好的。

require(ff)
x <- data.frame(id = 1:100000, timepoint = seq(from = Sys.time(), by = "sec", length.out = 100000))
x$timepoint <- as.factor(x$timepoint)

xff <- as.ffdf(x)
class(xff)
require(ffbase)
xff$time <- with(xff, as.POSIXct(as.character(timepoint)), by = 10000)
ramclass(xff$time)
[1] "POSIXct" "POSIXt" 
str(xff[1:10, ])
'data.frame':   10 obs. of  3 variables:
 $ id       : int  1 2 3 4 5 6 7 8 9 10
 $ timepoint: Factor w/ 100000 levels "2014-10-20 09:14:10",..: 1 2 3 4 5 6 7 8 9 10
 $ time     : POSIXct, format: "2014-10-20 09:14:10" "2014-10-20 09:14:11" "2014-10-20 09:14:12" "2014-10-20 09:14:13" ...

关于r - 如何在 ff 或 ffbase 中将因子向量转换为 POSIXct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26442248/

相关文章:

R - 仅按时间子集数据帧

r - 如何从此字符串中检索 POSIX 时间戳?

r - 如何按日期在 R 中对大型数据框 (ffdf) 进行子集化?

r - R : how to move data from one drive to another, 中的 ff 包并更改文件名

r - 如何将 google API key 传递给 get_maps?

r - R中情节标签中的 latex 和变量?

r - 在不引用完整路径的情况下访问工作目录之外的文件

r - as.Date 不保留 R 中的小时和分钟信息

r - 在 Shiny 页面中使用可变数量的输入字段

r - 如何将35GB数据加载到R中?