如何在R的应用中使类型转换无效(bit64示例) [英] How to void type conversion in R's apply (bit64 example)

查看:183
本文介绍了如何在R的应用中使类型转换无效(bit64示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些R代码中使用bit64软件包。我创建了一个由64位整数组成的向量
,然后尝试使用 sapply 遍历向量中的这些
整数。这是一个示例:

I am using the bit64 package in some R code. I have created a vector of 64 bit integers and then tried to use sapply to iterate over these integers in a vector. Here is an example:

v = c(as.integer64(1), as.integer64(2), as.integer64(3))
sapply(v, function(x){is.integer64(x)})
sapply(v, function(x){print(x)})

is.integer64(x) print(x)给出错误的
(或至少)意外的答案(错误和不正确的浮点值)。
我可以通过直接索引向量 c 来避免这种情况,但是我有
两个问题:

Both the is.integer64(x) and print(x) give the incorrect (or at least) unexpected answers (FALSE and incorrect float values). I can circumvent this by directly indexing the vector c but I have two questions:


  1. 为什么要进行类型转换?在这种情况下,R是否使用某些规则?

  2. 有什么办法可以避免这种类型转换?

TIA。

推荐答案

以下是 lapply 的代码:

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

现在检查:

!is.vector(v)
#TRUE

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

来自帮助( as.list)


除非参数已经是列表或$,否则可能会删除属性b $ b表达式。

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

包验证者应考虑为 as.list 编写一个方法。可能值得进行功能请求...

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

这篇关于如何在R的应用中使类型转换无效(bit64示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆