2014-10-18 49 views
3

在使用read.csv.ffdf讀取大型數據集之後,其中一列是時間。如2014-10-18 00:01:02,該列中有1百萬行。該欄是一個因素。如何將其轉換爲POSIXctff支持?簡單地使用as.POSIXct()只是將值變成NA如何將因子向量轉換爲ff或ffbase中的POSIXct

或者當我在開始讀取數據集時,我可以指定該列爲POSIXct

我的目標是獲得月和日(甚至小時)。所以我對除了轉換爲POSIXct之外的解決方案開放。

例如,我們有9由2臺,

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

兩列ID(數字類),和時間(因子類)

這裏是dput

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") 
+1

請提供數據的一小樣品與dput的'的輸出(頭(數據))' – 2014-10-18 16:56:33

+0

對於因子轉換,你需要先在列上做一個'as.character'。然後你可以將它傳遞給'as.POSIXct'。 – hrbrmstr 2014-10-18 17:26:52

+0

似乎應用as.character後,列仍然是因子類別。我認爲問題是ff不支持字符....也許我錯了... – 2014-10-18 17:48:54

回答

1

玩具示例中,您可以從包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" ... 
0

在數據中讀取時使用colClasses。 例如與您的兩列的例子:ID(數字類),和time(因子類):

test <- read.csv.ffdf(file="test.csv", header=T, first.rows=-1,colClasses = c("integer","POSIXct")) 
相關問題