Atom Vayalinkal
Atom Vayalinkal

Reputation: 2692

R/RStudio changes name of column when read from csv

I am trying to read in a file in R, using the following command (in RStudio):

fileRaw <- read.csv(file = "file.csv", header = TRUE, stringsAsFactors = FALSE)

file.csv looks something like this: file.csv

However, when it's read into R, I get:
fileRaw in RStudio

As you can see LOCATION is changed to ï..LOCATION for seemingly no reason.

I tried adding check.names = FALSE but this only made it worse, as LOCATION is now replaced with LOCATION. What gives?

How do I fix this? Why is R/RStudio doing this?

Upvotes: 2

Views: 454

Answers (2)

Danny_ds
Danny_ds

Reputation: 11406

There is a UTF-8 BOM at the beginning of the file. Try reading as UTF-8, or remove the BOM from the file.

The UTF-8 representation of the BOM is the (hexadecimal) byte sequence 0xEF,0xBB,0xBF. A text editor or web browser misinterpreting the text as ISO-8859-1 or CP1252 will display the characters  for this.

Edit: looks like using fileEncoding = "UTF-8-BOM" fixes the problem in RStudio.

Upvotes: 2

Atom Vayalinkal
Atom Vayalinkal

Reputation: 2692

Using fileEncoding = "UTF-8-BOM" fixed my problem and read the file with no issues.

Using fileEncoding = "UTF-8"/encoding = "UTF-8" did not resolve the issue.

Upvotes: 1

Related Questions