R - as.character返回的字符数比实际多

时间:2016-03-16 15:16:54

标签: r rounding precision

在某些数值上使用as.character()时遇到了一个有趣的行为。

说我有这个载体:

test <- seq(-1.5,1,0.02)

现在,如果我把它作为字符,它会产生:

as.character(test)
  [1] "-1.5"                "-1.48"               "-1.46"               "-1.44"               "-1.42"               "-1.4"                "-1.38"              
  [8] "-1.36"               "-1.34"               "-1.32"               "-1.3"                "-1.28"               "-1.26"               "-1.24"              
 [15] "-1.22"               "-1.2"                "-1.18"               "-1.16"               "-1.14"               "-1.12"               "-1.1"               
 [22] "-1.08"               "-1.06"               "-1.04"               "-1.02"               "-1"                  "-0.98"               "-0.96"              
 [29] "-0.94"               "-0.92"               "-0.9"                "-0.88"               "-0.86"               "-0.84"               "-0.82"              
 [36] "-0.8"                "-0.78"               "-0.76"               "-0.74"               "-0.72"               "-0.7"                "-0.68"              
 [43] "-0.66"               "-0.64"               "-0.62"               "-0.6"                "-0.58"               "-0.56"               "-0.54"              
 [50] "-0.52"               "-0.5"                "-0.48"               "-0.46"               "-0.44"               "-0.42"               "-0.4"               
 [57] "-0.38"               "-0.36"               "-0.34"               "-0.32"               "-0.3"                "-0.28"               "-0.26"              
 [64] "-0.24"               "-0.22"               "-0.2"                "-0.18"               "-0.16"               "-0.14"               "-0.12"              
 [71] "-0.0999999999999999" "-0.0800000000000001" "-0.0600000000000001" "-0.04"               "-0.02"               "0"                   "0.02"               
 [78] "0.04"                "0.0600000000000001"  "0.0800000000000001"  "0.1"                 "0.12"                "0.14"                "0.16"               
 [85] "0.18"                "0.2"                 "0.22"                "0.24"                "0.26"                "0.28"                "0.3"                
 [92] "0.32"                "0.34"                "0.36"                "0.38"                "0.4"                 "0.42"                "0.44"               
 [99] "0.46"                "0.48"                "0.5"                 "0.52"                "0.54"                "0.56"                "0.58"               
[106] "0.6"                 "0.62"                "0.64"                "0.66"                "0.68"                "0.7"                 "0.72"               
[113] "0.74"                "0.76"                "0.78"                "0.8"                 "0.82"                "0.84"                "0.86"               
[120] "0.88"                "0.9"                 "0.92"                "0.94"                "0.96"                "0.98"                "1"       

所以有一些值可以得到额外的数字,我宁愿没有。

我知道这是由于数字如何存储在R中,但我仍然希望有一个字符串向量对应于我在第一个向量中的数字(为了访问矩阵中的某些行和列) ,我需要名字相同)。

改为

as.character(round(test,digits=2))

解决了我的问题,但我并不完全确定它是万无一失的。有人有建议吗?还是有一些我忽略的东西?

谢谢!

0 个答案:

没有答案