如何在保留 R 中的列的同时从“整数”转换为“双精度”对象类型?

问题描述 投票:0回答:1

我希望我的数据是“双精度”而不是“整数”对象类别,以便能够与特定的 R 包一起使用,同时保留将数据分离到指定不同站点的不同列中。我尝试过使用 as.double() 函数,但这会创建一串数字,这些数字不会分成以前的列。有没有办法转换为双精度数以便能够使用此 R 包,同时保留当前行和列中数据的分类?

r type-conversion integer double
1个回答
0
投票

这是我能想到的所有方法

m <- matrix(as.integer(0:99), 10)[1:4, 1:4]
str(m)
#  int [1:4, 1:4] 0 1 2 3 10 11 12 13 20 21 ...

l <- list(

    m,
    apply(m, 2, as.numeric),
        
    "storage.mode<-"(m, "numeric"),
    # in-line version of:
    # storage.mode(m) <- "numeric"

    "mode<-"(m, "numeric"),
    # in-line version of:
    # mode(m) <- "numeric"
    
    "[<-"(m, as.numeric(m)),
    # in-line version of:
    # m[] <- as.numeric(m)
    
    "dim<-"(as.numeric(m), dim(m))
)

str(l)
# List of 6
#  $ : int [1:4, 1:4] 0 1 2 3 10 11 12 13 20 21 ...
#  $ : num [1:4, 1:4] 0 1 2 3 10 11 12 13 20 21 ...
#  $ : num [1:4, 1:4] 0 1 2 3 10 11 12 13 20 21 ...
#  $ : num [1:4, 1:4] 0 1 2 3 10 11 12 13 20 21 ...
#  $ : num [1:4, 1:4] 0 1 2 3 10 11 12 13 20 21 ...
#  $ : num [1:4, 1:4] 0 1 2 3 10 11 12 13 20 21 ...
© www.soinside.com 2019 - 2024. All rights reserved.