When I import a text file into R, I get a special character added to the first value of the first column

Sometimes, when I import a text file into R, I get the character "ï" ¿"added to the first value of the first column. Does anyone know why this is?

For example, a text file with the values:

2011_21,3130
2010_51,4153
2011_16,3168
2010_20,3945
2012_38,2099
2012_17,2436
2010_40,2090
2011_2 ,1462

output the following results in R:

1st I read the file in:

ts_data <- read.csv("yr_wk sales.csv", header=FALSE)
head(ts_data)

This is the returned data:

 V1   V2
1 2011_21 3130
2    2010_51 4153
3    2011_16 3168
4    2010_20 3945
5    2012_38 2099
6    2012_17 2436

Any good way to avoid this? Thank you for your help.

+5
source share
3 answers

I had this problem when I was working with a txt file in Microsoft Word. I copied the data from txt saved by MS Word to a new txt file using Notepad, and the problem was resolved.

+3
source

Ok, @ DarrenCook!

, " " . (http://cran.r-project.org/doc/manuals/R-data.html) .

, . , UTF-8:

ts_data <- read.table("yr_wk sales.csv", fileEncoding = "UTF-8")

, !

+2

You need to use the following:

ts_data <- read.csv("yr_wk sales.csv", fileEncoding="UTF-8-BOM", header=FALSE)
head(ts_data)
+1
source

All Articles