2017-02-15 68 views
1

Aparently ifelse completelly改變的數量和不保留輸入的數據類型ERROR:ifelse(d == 0,NA,d),其中d是integer64返回無義( 「數」 的形式)

例如:

library(bit64) 
d <- c(1:10,NA,12,0) %>% as.integer64() 
d %>% str 
Class 'integer64' num [1:13] 4.94e-324 9.88e-324 1.48e-323 1.98e-323 2.47e-323 ... 

ifelse(d==0,NA,d) %>% str 
num [1:13] 4.94e-324 9.88e-324 1.48e-323 1.98e-323 2.47e-323 ... 
ifelse(d==0,5,d) %>% str 
num [1:13] 4.94e-324 9.88e-324 1.48e-323 1.98e-323 2.47e-323 ... 
ifelse(d==5,50,d) %>% str 
num [1:13] 4.94e-324 9.88e-324 1.48e-323 1.98e-323 5.00e+01 ... 

上圖: - 數字輸出是無稽之談 - 格式的輸出是「民」

這是一個已知的問題嗎?

是否有另一個功能用來代替ifelse

EDIT1:帶有data.table操作

d <- data.table(a=c(1:10,NA,12,0) %>% as.integer64) 
d[a==0,a:=NA] 
d 
         a 
1:     1 
2:     2 
3:     3 
4:     4 
5:     5 
6:     6 
7:     7 
8:     8 
9:     9 
10:     10 
11:     NA 
12:     12 
13: 9218868437227407266 

#obve: still incorrect, NA was interpreted as a nonsense integer64 

#now this works: 
    d <- data.table(a=c(1:10,NA,12,0) %>% as.integer64) 
d[a==0,a:=as.integer64(NA)] 
d 
    a 
1: 1 
2: 2 
3: 3 
4: 4 
5: 5 
6: 6 
7: 7 
8: 8 
9: 9 
10: 10 
11: NA 
12: 12 
13: NA 

EDIT2替換ifelse:tryed pading ifelse as.integer64的輸出()。不起作用!

d <- c(1:10,NA,12,0) %>% as.integer64() 
ifelse(d==0,as.integer64(NA),as.integer64(d)) 
[1] 4.940656e-324 9.881313e-324 1.482197e-323 1.976263e-323 2.470328e-323 2.964394e-323 3.458460e-323 3.952525e-323 
[9] 4.446591e-323 4.940656e-323   NA 5.928788e-323 0.000000e+00 

編輯3:添加的源包(bit64)

+0

哪個軟件包用於'integer64'(bit64?)? –

+1

對ifelse的不足之處的總結總結在這裏,同時討論一個新的ifelse2版本:https://stat.ethz.ch/pipermail/r-devel/2016-August/072970.html –

+0

@RYoda:是的,我正在使用bit64軟件包(剛更新了這個問題 – LucasMation

回答

3

bit64包中的數據類型integer64在內部使用可用R中的唯一的64位的數據類型:double

這意味着一些目前實施該方案的限制,例如,摹:

  • str:從基礎R繼承,不打印值正確
  • NA必須NA_integer64_

被替換請閱讀更多細節的文檔:https://cran.r-project.org/web/packages/bit64/bit64.pdf

要讓您的示例使用ifelse您必須

  1. integer64類分配給的ifelse結果再次(因爲它失去了應有的known deficienciesifelse)和
  2. 使用NA_integer64_代替NA

這樣的:

library(bit64) 
library(dplyr) 
d <- c(1:10,NA_integer64_,12,0) %>% as.integer64() 
d 
# integer64 
# [1] 1 2 3 4 5 6 7 8 9 10 0 12 0 
class(d) 
# [1] "integer64" 
e <- ifelse(d==0,NA_integer64_,d) 
e 
# [1] 4.940656e-324 9.881313e-324 1.482197e-323 1.976263e-323 2.470328e-323 2.964394e-323 3.458460e-323 3.952525e-323 4.446591e-323 
# [10] 4.940656e-323 0.000000e+00 5.928788e-323 0.000000e+00 
class(e) 
# [1] "numeric" 
class(e) <- "integer64" 
e 
# integer64 
# [1] 1 2 3 4 5 6 7 8 9 10 <NA> 12 <NA> 

如果您現在撥打str(這是而不是支持bit64)您將再次看到垃圾:

> str(e)  # same as: e %>% str 
Class 'integer64' num [1:13] 4.94e-324 9.88e-324 1.48e-323 1.98e-323 2.47e-323 ... 
相關問題