R:有没有办法绑定非数字列

R: is there a way to cbind non-numeric columns

library(Rmpfr)
mynumber <- new("mpfr", .Data = list(new("mpfr1", prec = 50L, exp = c(1045L, 
0L), sign = 1L, d = c(151748608L, -358118319L)), new("mpfr1", 
    prec = 50L, exp = c(20L, 0L), sign = 1L, d = c(-1114947584L, 
    -1905679017L)), new("mpfr1", prec = 50L, exp = c(-55L, -1L
), sign = 1L, d = c(-1449918464L, -906197701L)), new("mpfr1", 
    prec = 50L, exp = c(221L, 0L), sign = 1L, d = c(819707904L, 
    -1329031570L))))

mynumber 是一个 class mpfr 对象,里面有 4 个数字。我想 cbind mynumber 列为 0,即

> cbind(rep(0, 4), mynumber)
       mynumber
[1,] 0 ?       
[2,] 0 ?       
[3,] 0 ?       
[4,] 0 ?  

这给了我????在第二列中,所以我尝试先将 mynumber 更改为 class numeric

mydata <- cbind(rep(0, 4), sapply(mynumber, asNumeric))
> mydata
     [,1]         [,2]
[1,]    0          Inf
[2,]    0 5.833223e+05
[3,]    0 2.189941e-17
[4,]    0 2.327185e+66

但是,由于 mynumber 中的第一个数字非常大,因此使用 asNumeric 将其改为 Inf

编辑: 我的最终目标是 运行:

mydata <- cbind(rep(0, 4), sapply(mynumber, asNumeric))
> mydata/rowSums(mydata)
     [,1] [,2]
[1,]    0  NaN
[2,]    0    1
[3,]    0    1
[4,]    0    1

并且没有让它打印出 NaN。

一个选项是用 list 包装,然后创建一个 tibble/data.frame 对象,因为 cbind 转换为 matrixmatrix 只能容纳一个单身class

library(tibble)
tibble(col1 = 0, col2 = list(mynumber))
# A tibble: 1 x 2
#   col1 col2  
#  <dbl> <list>
#1     0 <mpfr>

cbind 甚至 characternumeric class returns character 对于所有列,这不是一个好的选择当要绑定的 vector 不同时 class

cbind(letters[1:4], 1:4)

通过在加载包后检查 methods for cbind

methods('cbind')
#[1] cbind,ANY-method     cbind,Mnumber-method cbind.bigq*          cbind.bigz*         
#[5] cbind.data.frame     cbind.grouped_df*    cbind.ts*       

所以,如果它对 Mnumber 使用了正确的 cbind 方法,它就不应该给出 Inf

cbind(rep(0,4),  mynumber)
#'mpfrMatrix' of dim(.) =  (4, 2) of precision  50 .. 53  bits 
#     [,1] [,2]                   
#[1,]   0. 3.4556867084990952e+314
#[2,]   0.      583322.33392099757
#[3,]   0.  2.1899410233914937e-17
#[4,]   0.  2.3271850367397449e+66

或者利用回收价值

cbind(0,  mynumber)
#'mpfrMatrix' of dim(.) =  (4, 2) of precision  50 .. 53  bits 
#     [,1] [,2]                   
#[1,]   0. 3.4556867084990952e+314
#[2,]   0.      583322.33392099757
#[3,]   0.  2.1899410233914937e-17
#[4,]   0.  2.3271850367397449e+66

此外,如果我们检查屏蔽的功能,当我们加载包时,它说

The following objects are masked from ‘package:base’:

cbind, pmax, pmin, rbind

通过使用 base 中的 cbind,可以复制 ?。对于 OP,cbind 可能来自 base

base::cbind(0, mynumber)
#       mynumber
#[1,] 0 ?       
#[2,] 0 ?       
#[3,] 0 ?       
#[4,] 0 ?       

如果 Rmpfr 中的 cbind 被屏蔽,则使用 ::

mydata <- Rmpfr::cbind(0, mynumber)
mydata
#'mpfrMatrix' of dim(.) =  (4, 2) of precision  50 .. 53  bits 
#     [,1] [,2]                   
#[1,]   0. 3.4556867084990952e+314
#[2,]   0.      583322.33392099757
#[3,]   0.  2.1899410233914937e-17
#[4,]   0.  2.3271850367397449e+66




mydata/rowSums(mydata)
#'mpfrMatrix' of dim(.) =  (4, 2) of precision  53   bits 
#     [,1] [,2]              
#[1,]   0. 1.0000000000000000
#[2,]   0. 1.0000000000000000
#[3,]   0. 1.0000000000000000
#[4,]   0. 1.0000000000000000

不知道是不是你创建的对象有问题。当我更改它以确保每个 exp 字段只有一个值时,我得到了想要的结果:

library(Rmpfr)

x <- new("mpfr", .Data = list(new("mpfr1", prec = 50L, exp = 1045L, 
    sign = 1L, d = c(151748608L, -358118319L)), new("mpfr1", 
    prec = 50L, exp = 20L, sign = 1L, d = c(-1114947584L, -1905679017L
    )), new("mpfr1", prec = 50L, exp = -55L, sign = 1L, d = c(-1449918464L, 
-906197701L)), new("mpfr1", prec = 50L, exp = 221L, sign = 1L, 
    d = c(819707904L, -1329031570L))))

cbind(c(0, 0, 0, 0), x)
#> 'mpfrMatrix' of dim(.) =  (4, 2) of precision  50 .. 53  bits 
#>      [,1] [,2]                   
#> [1,]   0. 3.4556867084990952e+314
#> [2,]   0.      583322.33392099757
#> [3,]   0.  2.1899410233914937e-17
#> [4,]   0.  2.3271850367397449e+66

reprex package (v0.3.0)

于 2020-02-29 创建
zeros <- mpfr(c(0,0,0,0), precBits = getPrec(mynumber))
M <- cbind(mynumber, zeros)
M %*% t(M) # just to try