rnorouzian
rnorouzian

Reputation: 7517

The column name changes after importing the data into R

The first column in my data below is called ID. But I wonder why when I import my data into R, the first column names is shown as ï..ID?

How can can keep ID to be the column name?

d <- read.csv("https://raw.githubusercontent.com/rnorouzian/v/main/memory.csv")

names(d)

# [1] "ï..ID" "Group" "MP"    "SE"

Upvotes: 2

Views: 766

Answers (2)

ppoyk
ppoyk

Reputation: 43

Likely the correct solution for this is to check for the encoding of the csv file being read. This can be done by using Notepad++, for example.

The encoding of the file has to be supplied as a string to the read.csv function via the fileEncoding argument:

df <- read.csv("file/path.csv", fileEncoding = "ENCODING_STRING")

I am not certain if all possible encoding types are supported, but in my case supplying the correct "UTF-8-BOM" encode type fixed the exact problem you presented of "ï.." being prepended at the start of the 1st column name. This most likely has something to do with how the file bits are being read depending on the encoding.

I would resort to check.names = F or renaming the column in code only if supplying the correct encoding type does not solve the issue.

Upvotes: 0

akrun
akrun

Reputation: 887951

We can specify check.names = FALSE in case there are some characters that are unusual

d <- read.csv("https://raw.githubusercontent.com/rnorouzian/v/main/memory.csv", check.names = FALSE)

names(d)
#[1] "ID"    "Group" "MP"    "SE"   

Upvotes: 1

Related Questions