我是r的新手,我首先有此表,我尝试使用df <-read.csv(“ geral_modelo_inadimplencia_2.csv”,sep =“;”,stringsAsFactors = FALSE)打开文件,导致所有数据在chr之后确实尝试将as.numeric转换为数字,但返回[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA。 如何将chr转换为数字?
Saldo.Rotativo A.Vista Parcelado Saque
1 dez/16 2.919.869.506,96 6.364.901.107,55 463.677.827,46 729.185,18
2 dez/17 2.007.351.784,18 6.831.919.805,09 780.093.428,86 2.817.814,72
3 dez/18 1.944.752.969,68 7.458.695.484,62 542.074.530,21 1.885.633,29
4 jan/19 1.991.796.619,57 7.371.837.099,11 540.893.516,33 2.058.371,60
5 fev/19 2.121.650.035,30 6.967.735.692,25 562.211.487,01 2.132.249,61
6 mar/19 2.062.475.653,11 6.900.028.117,67 575.861.976,61 2.100.849,74
7 abr/19 2.089.793.039,24 6.945.593.710,28 583.181.387,89 2.090.951,69
8 mai/19 2.078.700.800,99 7.146.597.703,16 612.184.578,96 2.132.951,04
9 jun/19 2.239.390.093,82 6.851.118.033,80 618.844.690,37 1.764.866,10
10 jul/19 2.121.263.409,38 7.196.087.606,84 629.995.715,52 3.945.650,40
11 ago/19 2.173.187.280,54 7.089.675.942,22 624.808.459,45 6.341.527,95
12 set/19 2.285.571.063,90 7.111.228.186,19 617.840.220,61 6.143.505,16
13 out/19 2.193.401.889,85 7.263.912.266,04 622.821.392,86 7.253.169,67
14 nov/19 2.281.061.211,60 7.240.713.335,11 611.161.428,40 7.484.398,11
15 dez/19 2.212.531.321,45 7.892.016.606,72 597.916.084,63 6.464.980,78
We can use
str_remove_all
数据
Unfortunately, R doesn't appear to have a base function that automatically detects the different-locale, so we need a pair of
gsub
s.While not base-R, akrun's original suggestion of
readr::parse_number
(since deleted) can be made to do it "properly":因此,虽然不一定更快(我还没有对其进行基准测试),但您可能会认为这是一种干净的声明性代码:
(output
str
'd and truncated for brevity.)数据:
(I inferred
rowname
, since it was not obvious in your question. When there's any ambiguity, it helps to usedput
.)