How to void type conversion in R's apply (bit64 example)

六眼飞鱼酱① 提交于 2019-12-01 20:28:48

Here is the code of lapply:

function (X, FUN, ...) 
{
    FUN <- match.fun(FUN)
    if (!is.vector(X) || is.object(X)) 
        X <- as.list(X)
    .Internal(lapply(X, FUN))
}

Now check this:

!is.vector(v)
#TRUE

as.list(v)
#[[1]]
#[1] 4.940656e-324
#
#[[2]]
#[1] 9.881313e-324
#
#[[3]]
#[1] 1.482197e-323

From help("as.list"):

Attributes may be dropped unless the argument already is a list or expression.

So, either you creaste a list from the beginning or you add the class attributes:

v_list <- lapply(as.list(v), function(x) {
  class(x) <- "integer64"
  x
  })

sapply(v_list, function(x){is.integer64(x)})
#[1] TRUE TRUE TRUE

The package authours should consider writing a method for as.list. Might be worth a feature request ...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!