将字符矩阵转换为数字矩阵

 偏偏喜欢你_Jerry_207 发布于 2023-02-08 01:42

我有一个7乘31字符矩阵调用extra4,其结构如下所示:

> str(extra4)
 chr [1:7, 1:31] "36.88  " " 45.48  " " 52.46  " " 111.31 " " 138.45 " " 121.09 " " 122.62" ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:7] "1990" "1991" "1992" "1993" ...
  ..$ : chr [1:31] "1" "2" "3" "4" ...

在阅读了SO中的类似问题后,我尝试了以下但是我失败了:

>matrix(as.numeric(unlist(extra4)),nrow=nrow(extra4))
Warning message:
In matrix(as.numeric(unlist(extra4)), nrow = nrow(extra4)) :
  NAs introduced by coercion

而且我也试过了

> class(extra4)<-"numeric"
Warning message:
In class(extra4) <- "numeric" : NAs introduced by coercion

> extra4<-apply(extra4, 1, as.numeric)
Warning messages:
1: In apply(extra4, 1, as.numeric) : NAs introduced by coercion
2: In apply(extra4, 1, as.numeric) : NAs introduced by coercion
3: In apply(extra4, 1, as.numeric) : NAs introduced by coercion
4: In apply(extra4, 1, as.numeric) : NAs introduced by coercion
5: In apply(extra4, 1, as.numeric) : NAs introduced by coercion
6: In apply(extra4, 1, as.numeric) : NAs introduced by coercion
7: In apply(extra4, 1, as.numeric) : NAs introduced by coercion

> extra4<-apply(extra4, 2, as.numeric)
There were 31 warnings (use warnings() to see them)

我也尝试将矩阵更改为数据框然后执行,sapply(extra4, as.numeric)但这也不起作用,我也尝试将数据写为csv,但不知何故输出最终包括非数字字符.

这很奇怪,因为特别是在完成上述操作后,只有部分数字转为数字值.但是,我确信所有元素都是字符,因为当我比较那些被保存的和那些不存在的元素时,我得到了

> str(extra4[1,1])
 chr "36.88  "
> str(extra4[1,2])
 chr " 19.11  "

我还添加了以下内容以更详细地显示我的数据:

> dput(extra4)
structure(c("36.88  ", " 45.48  ", " 52.46  ", " 111.31 ", 
" 138.45 ", " 121.09 ", " 122.62", " 19.11  ", " 27.97  ", 
" 37.14  ", " 47.68  ", " 60.78  ", " 35.84  ", " 38.64", 
" 56.21  ", " 74.94  ", " 92.3   ", " 118.62 ", " 138.13 ", 
" 104.65 ", " 113.98", " 30.48  ", " 51.54  ", " 61.57  ", 
" 99.87  ", " 80.9   ", " 84.97  ", " 99.34", "20.16  ", 
" 24.76  ", " 27.76  ", " 37.53  ", " 50.53  ", " 28.8   ", 
" 25.06", " 87.73  ", " 98.68  ", " 119.95 ", " 150.74 ", 
" 214.35 ", " 118.5  ", " 129.19", " 32.36  ", " 36.52  ", 
" 42.67  ", " 56.55  ", " 89.22  ", " 49.97  ", " 50.62", 
"35.09  ", " 40.77  ", " 48.43  ", " 82.61  ", " 120.1  ", 
" 72.43  ", " 76.69", " 47.21  ", " 67.25  ", " 78.62  ", 
" 66.64  ", " 83.78  ", " 127.79 ", " 154.11", " 86.1   ", 
" 127.59 ", " 164.43 ", " 249.32 ", " 312.01 ", " 272.09 ", 
" 265.68", " 83.75  ", " 118.41 ", " 171.52 ", " 229.27 ", 
" 241.63 ", " 201    ", " 213.01", " 36.63  ", " 52.1   ", 
" 66.03  ", " 101.38 ", " 126.71 ", " 95.46  ", " 110.03", 
" 57.5   ", " 75.72  ", " 101.31 ", " 147.5  ", " 171.01 ", 
" 148.66 ", " 167.93", " 29.56  ", " 38.37  ", " 48.8   ", 
" 65.5   ", " 84.77  ", " 75.2   ", " 81.27", " 77.28  ", 
" 93.7   ", " 119.62 ", " 247    ", " 301.76 ", 
" 222.52 ", " 244.46", " 45.6   ", " 54.32  ", " 87.81  ", 
" 132.93 ", " 163.62 ", " 152.99 ", " 170.85", " 27.13  ", 
" 36.96  ", " 48.94  ", " 80.01  ", " 124.07 ", " 93.49  ", 
" 105.57", " 54.55  ", " 85.93  ", " 102.3  ", " 122.7  ", 
" 168.36 ", " 151.79 ", " 169.65", " 86.19  ", " 121.82 ", 
" 191.7  ", " 247.75 ", " 260.23 ", " 196.48 ", " 243.06", 
"47.35  ", " 60.63  ", " 76.4   ", " 93.04  ", " 102.13 ", 
" 98.29  ", " 86.27", " 10.93  ", " 13.33  ", " 16.82  ", 
" 18.2   ", " 23.48  ", " 16.52  ", " 16.19", "   NA   ", 
"  NA    ", "   NA   ", "  NA    ", " 69.46  ", 
" 54.22  ", " 60.16", " 60.93  ", " 89.86  ", " 141.85 ", 
" 207.9  ", " 182.79 ", " 159.1  ", " 159.46", " 15.37  ", 
" 18.48  ", " 24.33  ", " 38.37  ", " 45.87  ", " 34.86  ", 
" 31.96", " 34.05  ", " 40.1   ", " 55.02  ", " 58.31  ", 
" 86.89  ", " 65.68  ", " 65.68", "1.51   ", " 0.93   ", 
" 1      ", " 1.78   ", " 2.8    ", " 1.56   ", 
" 1.41", " 27.15  ", " 31.37  ", " 39.46  ", " 40.33  ", 
" 61.86  ", " 45.18  ", " 57.71", " 14.74  ", " 16.3   ", 
" 25.06  ", " 31.74  ", " 37.39  ", " 27.18  ", " 30.49", 
" 3.59   ", " 4.86   ", " 5.67   ", " 6.36   ", 
" 7.6    ", " 4.8    ", " 5.5", "4.73   ", " 5.68   ", 
" 7.3    ", " 8.53   ", " 11.03  ", " 8.44   ", 
" 9.84", "16.76  ", " 24.83  ", " 32.66  ", " 46.22  ", 
" 48.01  ", " 43.44  ", " 48.29"), .Dim = c(7L, 31L), .Dimnames = list(
    c("1990", "1991", "1992", "1993", "1994", "1995", "1996"), 
    c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
    "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", 
    "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"
    )))

sessionInfo() 给出了以下内容:

    > sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gdata_2.13.2

loaded via a namespace (and not attached):
[1] gtools_2.7.1 tools_3.0.0 

Reinstate Mo.. 35

这里根本没有问题,不是我尝试的大多数选项.你收到了警告,但这些都与"NA"字符串有关,因为它们不是NA字符串中存储的数字,R不知道如何处理它们并将它们更改为NA.这是所有警告告诉你的.于是

apply(extra4, 2, as.numeric)
sapply(extra4, as.numeric)
class(extra4) <- "numeric"
storage.mode(extra4) <- "numeric"

所有工作和所有人都警告第22栏中的"NA"值(或其变体)extra4:

Warning message:
In storage.mode(m) <- "numeric" : NAs introduced by coercion

但这些只是警告,在这种情况下可以忽略.如果他们麻烦你,你可以打电话suppressWarnings()

> suppressWarnings(storage.mode(m) <- "numeric")

但这很危险,因为它会阻止所有警告,而不仅仅是关于NAs的警告.

1 个回答
  • 这里根本没有问题,不是我尝试的大多数选项.你收到了警告,但这些都与"NA"字符串有关,因为它们不是NA字符串中存储的数字,R不知道如何处理它们并将它们更改为NA.这是所有警告告诉你的.于是

    apply(extra4, 2, as.numeric)
    sapply(extra4, as.numeric)
    class(extra4) <- "numeric"
    storage.mode(extra4) <- "numeric"
    

    所有工作和所有人都警告第22栏中的"NA"值(或其变体)extra4:

    Warning message:
    In storage.mode(m) <- "numeric" : NAs introduced by coercion
    

    但这些只是警告,在这种情况下可以忽略.如果他们麻烦你,你可以打电话suppressWarnings()

    > suppressWarnings(storage.mode(m) <- "numeric")
    

    但这很危险,因为它会阻止所有警告,而不仅仅是关于NAs的警告.

    2023-02-08 02:30 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有