How to load df with 1000 separator in R as numeric class?

前端 未结 3 525
猫巷女王i
猫巷女王i 2020-12-22 04:26

I have a UTF-16 Unicode Text (.txt) file downloaded and defaulted as comma-separated values (.csv) when saved on a mac drive. This file contains numeric data that has 1000 s

3条回答
  •  误落风尘
    2020-12-22 05:10

    I suspect that gsub doesn't work right on your UTF-16 strings. Perhaps you should convert the strings before doing the substitution. Try the following:

    tx <- read.table("/Users/username/Desktop/report.csv",sep="\t", dec = ".", fileEncoding = "UTF-16LE", fill = T, skip=1 , quote="", header=T, stringsAsFactors = FALSE)
    tx$Cost <- iconv(tx$Cost,"UTF-16","ASCII",sub='')
    tx$Cost <- gsub("\\,", replacement = "", x = tx$Cost)
    tx$Cost <- as.numeric(tx$Cost)
    

提交回复
热议问题