[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
新手(沒寫過程式,R 是我的第一次)
[問題敘述]:
大家好,我的問題是在 convert factor to numeric 時,會將遇到超過1,000的數值變成
NA,爬過網路上不少文章,集合了幾種方法都無效。
#convert a factor to a numeric
##way1
library(varhandle)
unfactor(f)
##way2
as.numeric(levels(f))[f]
as.numeric(levels(f))[as.integer(f)]
as.numeric(as.character(f))
##way3
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
##way4
,stringsAsFactors=FALSE
[程式範例]:
https://pastebin.com/2DQDxYAn
請注意第18個數值"1,130.00"變成了NA。
[環境敘述]:
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese
(Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] survival_2.41-3
loaded via a namespace (and not attached):
[1] compiler_3.4.4 Matrix_1.2-12 tools_3.4.4 yaml_2.1.18
splines_3.4.4
[6] grid_3.4.4 lattice_0.20-35
[關鍵字]:
factor,as.numeric