2012-11-22 78 views
2

我正在使用在某些列中具有NAs的ffdf對象。 NAs是使用merge.ffdf進行左外部合併的結果。我想用0代替NA,但不管理它。 這裏是代碼我運行:在ffdf對象中替換NA

library(ffbase) 
    deals <- merge(deals,rk,by.x=c("DEALID","STICHTAG"),by.y=c("ID","STICHTAG"),all.x=TRUE) 
    attributes(deals) 
    $names 
    [1] "virtual" "physical" "row.names" 
    $class 
    [1] "ffdf" 

vmode(deals$CREDIT_R) 
[1] "double" 

    idx <- ffwhich(deals,is.na(CREDIT_R)) # CREDIT_R is one of the columns with NAs 
    deals.strom[idx,"CREDIT_R"]<-0 
    error in `[<-.ffdf`(`*tmp*`, idx, "CREDIT_R", value = 0) : 
     ff/ffdf-iness of value and selected columns don't match 

任何想法,我做錯了什麼?總的來說,我想了解更多有關替換ff和ffdf類的方法。任何可以找到關於這個主題的例子的建議?

回答

4

軟件包ff的手冊表示一個名爲ffindexset的函數。

idx <- is.na(deals$CREDIT_R) ## This uses is.na.ff_vector from ffbase 
idx <- ffwhich(idx, idx == TRUE) ## Is part of ffbase 
deals$CREDIT_R <- ffindexset(x=deals$CREDIT_R, index=idx, value=ff(0, length=length(idx), vmode = "double")) ## Is part of ff 
deals$CREDIT_R[idx] <- ff(0, length=length(idx), vmode = "double") ## this one will probably also work 

也看看Extract.ff

+0

嗨jwijffels,感謝您的答覆,但是你提出產生於我而言同樣的錯誤味精這兩個解決方案:*錯誤:長度(值)==ñ不是真的* – ddg

+1

對,我忘了添加長度,我已經更新了答案 – jwijffels

+1

嗨,這兩個解決方案都在工作。一如既往的非常快速和準確的答案。非常感謝! – ddg