Chris
Chris

Reputation: 87

Why does the column name change after open the dataset in R?

I imported an excel dataset in R but there is one column that its name changed a little bit, just as shown below:

enter image description here

The original name of the first column is "Id" but it seems to change a little bit. And I make sure I didn't change anything in my original dataset. Just imported it in R and opened, it looks like this. What happened? Thanks many in advance!

Upvotes: 1

Views: 1938

Answers (1)

akrun
akrun

Reputation: 887951

While reading the dataset use the check.names = FALSE in read.csv/read.table to prevent the checks of column names

dat <- read.csv("file.csv", check.names = FALSE)

check.names = TRUE (default option) triggers make.names and make.unique that changes the column names if it doesn't conform to standard format, i.e. it would append X at the beginning if the column names start with numbers...


If we check the source code of read.table

 ...

 if (check.names) 
        col.names <- make.names(col.names, unique = TRUE)

 ...

and make.names calls make.unique

make.names
function (names, unique = FALSE, allow_ = TRUE) 
{
    names <- as.character(names)
    names2 <- .Internal(make.names(names, allow_))
    if (unique) {
        o <- order(names != names2)
        names2[o] <- make.unique(names2[o])
    }
    names2
}

Upvotes: 2

Related Questions