今天遇到了一个问题就是,如果数据里面有逗号,那么如何转换他们。就像下面的这样:
> exercise9_1$地区生产总值 [1] 16,251.93 11,307.28 24,515.76 11,237.55 14,359.88 22,226.70 10,568.83 [8] 12,582.00 19,195.69 49,110.27 32,318.85 15,300.65 17,560.18 11,702.82 [15] 45,361.85 26,931.03 19,632.26 19,669.56 53,210.28 11,720.87 2,522.66 [22] 10,011.37 21,026.68 5,701.84 8,893.12 605.83 12,512.30 5,020.37 [29] 1,670.44 2,102.21 6,610.05 31 Levels: 1,670.44 10,011.37 10,568.83 11,237.55 11,307.28 ... 8,893.12
#哈哈哈,可以看出这个是因子,我说我怎么画不了图……
16,251.93里面有个逗号,一般来说,可以用excel来转换,但是,本人有强迫症,后来在网上搜索到可以用gsub()函数来处理,
> number <- c("11,307.28") > mode(number) [1] "character" > (test1 <- gsub(",","",number)) [1] "11307.28" > mode(test1) [1] "character" > (test2 <- as.numeric(test1)) [1] 11307.28 > mode(test2) [1] "numeric"
#这里已经是数值型了
接着上面处理一下exercise9_1$地区生产总值,
> (exercise9_1$地区生产总值 <- as.numeric(gsub(",","",exercise9_1$地区生产总值))) [1] 16251.93 11307.28 24515.76 11237.55 14359.88 22226.70 10568.83 12582.00 [9] 19195.69 49110.27 32318.85 15300.65 17560.18 11702.82 45361.85 26931.03 [17] 19632.26 19669.56 53210.28 11720.87 2522.66 10011.37 21026.68 5701.84 [25] 8893.12 605.83 12512.30 5020.37 1670.44 2102.21 6610.05