涉及“”时如何将data.frame列转换为数字

时间:2019-02-17 20:15:33

标签: r

我需要使用read.table函数来导入数据。数据在.txt文件中,我的“销售”数字输入在“引号”中。当我尝试转换时,得到NA。我只需要转换此列。

尝试转换为单个字符串或矩阵以消除“”

car<-read.table("C:/Desktop/STA 675/HW3/cardata_hw3.txt",header = FALSE, 
stringsAsFactors = FALSE)
car
car1<- 


data.frame(Year=car$V1,Country=car$V2,Type=car$V3,Sales=car$V4,Quarter=car$V5,stringsAsFactors = FALSE)


head(car1,n=7)

xxx<-as.numeric(car1$Sales)

## Sales data has to be in Numeric, not chr, however, when I convert:
NAs introduced by coercion


DATA I am working off: 
2013    US  CARS    "213,504.05"    Second
2013    Us  Cars    "328,450.45"    Forth
2013    US  TRUCKS  "116,735.65"    Second
2013    Us  Trucks  "253,945.62"    Forth
2013    CANADA  CARS    ?   Second

0 个答案:

没有答案