我不知道这是一个integer64问题(来自bit64),还是一个熔化问题(来自reshape2,但是如果我试图重塑一个包含integer64数据的data.frame,那么类信息在这个过程中会被破坏,它会恢复为双重表示:
library(bit64)
library(reshape2)
DF = data.frame(I =letters, Num1 = as.integer64(1:26), Num2 = as.integer64(1:26))
DFM = melt(DF, id.vars = "I")
sapply(DF, class)
sapply(DFM, class)提供:
> sapply(DF, class)
I Num1 Num2
"factor" "integer64" "integer64"
> sapply(DFM, class)
I variable value
"factor" "factor" "numeric" 而且因为integer64是双层的,所以数据是“损坏的”。
> DF
I Num1 Num2
1 a 1 1
2 b 2 2
3 c 3 3
4 d 4 4
5 e 5 5
...
> DFM
I variable value
1 a Num1 4.940656e-324
2 b Num1 9.881313e-324
3 c Num1 1.482197e-323
4 d Num1 1.976263e-323
5 e Num1 2.470328e-323
6 f Num1 2.964394e-323是什么导致了这种情况?这是integer64问题还是melt问题?在创建类时,可以做什么来避免这种事情?
发布于 2013-02-15 19:28:47
这似乎是包的一个限制,在他们的文档中也提到了。例如:
x <- data.frame(a=as.integer64(1:5), b=as.integer64(1:5))
> x
# a b
# 1 1 1
# 2 2 2
# 3 3 3
# 4 4 4
# 5 5 5
> unlist(x)
# a1 a2 a3 a4 a5 b1
# 4.940656e-324 9.881313e-324 1.482197e-323 1.976263e-323 2.470328e-323 4.940656e-324
# b2 b3 b4 b5
# 9.881313e-324 1.482197e-323 1.976263e-323 2.470328e-323
> as.matrix(x)
# a b
# [1,] 4.940656e-324 4.940656e-324
# [2,] 9.881313e-324 9.881313e-324
# [3,] 1.482197e-323 1.482197e-323
# [4,] 1.976263e-323 1.976263e-323
# [5,] 2.470328e-323 2.470328e-323
x <- as.integer64(1:5)
> is.vector(x)
# [1] FALSE
> as.vector(x)
# [1] 4.940656e-324 9.881313e-324 1.482197e-323 1.976263e-323 2.470328e-323发布于 2013-02-15 19:29:29
重置类似乎可以“纠正”结果,如下所示。但是,正如在讨论中提到的,如果数值还包含integer64以外的其他类型,则这很可能不起作用。
> class(DFM$value) <- "integer64"
> DFM
I variable value
1 a Num1 1
2 b Num1 2
3 c Num1 3发布于 2013-02-15 19:04:00
我也可以复制它。
不是一个解决方案,但问题似乎发生在melt.data.frame函数的以下行:
value <- unlist(unname(data[var$measure]))在您的示例中,这将导致:
unlist(unname(DF[c("Num1","Num2")]))unlist调用改变了数据的类。正如帮助页面所说的:
The output type is determined from the highest type of the
components in the hierarchy NULL < raw < logical < integer < real
< complex < character < list < expression, after coercion of
pairlists to lists.https://stackoverflow.com/questions/14892857
复制相似问题