78

I asked a question about this a few months back, and I thought the answer had solved my problem, but I ran into the problem again and the solution didn't work for me.

I'm importing a CSV:

orders <- read.csv("<file_location>", sep=",", header=T, check.names = FALSE)

Here's the structure of the dataframe:

str(orders)

'data.frame':   3331575 obs. of  2 variables:
 $ OrderID  : num  -2034590217 -2034590216 -2031892773 -2031892767 -2021008573 ...
 $ OrderDate: Factor w/ 402 levels "2010-10-01","2010-10-04",..: 263 263 269 268 301 300 300 300 300 300 ...

If I run the length command on the first column, OrderID, I get this:

length(orders$OrderID)
[1] 0

If I run the length on OrderDate, it returns correctly:

length(orders$OrderDate)
[1] 3331575

This is a copy/paste of the head of the CSV.

OrderID,OrderDate
-2034590217,2011-10-14
-2034590216,2011-10-14
-2031892773,2011-10-24
-2031892767,2011-10-21
-2021008573,2011-12-08
-2021008572,2011-12-07
-2021008571,2011-12-07
-2021008570,2011-12-07
-2021008569,2011-12-07

Now, if I re-run the read.csv, but take out the check.names option, the first column of the dataframe now has an X. at the start of the name.

orders2 <- read.csv("<file_location>", sep=",", header=T)

str(orders2)

'data.frame':   3331575 obs. of  2 variables:
 $ X.OrderID: num  -2034590217 -2034590216 -2031892773 -2031892767 -2021008573 ...
 $ OrderDate: Factor w/ 402 levels "2010-10-01","2010-10-04",..: 263 263 269 268 301 300 300 300 300 300 ...

length(orders$X.OrderID)
[1] 3331575

This works correctly.

My question is why does R add an X. to beginning of the first column name? As you can see from the CSV file, there are no special characters. It should be a simple load. Adding check.names, while will import the name from the CSV, will cause the data to not load correctly for me to perform analysis on.

What can I do to fix this?

Side note: I realize this is a minor - I'm just more frustrated by the fact that I think I am loading correctly, yet not getting the result I expected. I could rename the column using colnames(orders)[1] <- "OrderID", but still want to know why it doesn't load correctly.

smci
  • 32,567
  • 20
  • 113
  • 146
mikebmassey
  • 8,354
  • 26
  • 70
  • 95
  • Can you cut and paste the following outputs: `head(orders)` & `head(orders2)`? – Tyler Rinker May 04 '12 at 01:19
  • 3
    I'm more curious to see the actual raw csv file. Can you post it somewhere and provide a link so we can download it and try to reproduce this behavior. Whatever the problem is, my guess is the answer lies in the precise structure and contents of the file. – joran May 04 '12 at 01:21
  • I don't get the str of orders but then the `length(orders$OrderID) [1]0` – Tyler Rinker May 04 '12 at 02:01
  • 6
    I'm with @joran; I imagine that there is a non-visible character at the start of the file which is being pulled into the column name (with `check.names=FALSE`) or triggering a name change (with `check.names=TRUE`). Unfortunately, a cut-and-past of the CSV probably won't show that. What does `dput(names(orders)[1])` give? Also, if `length(orders[[1]])` gives the right value, then you know it is in the name. – Brian Diggs May 04 '12 at 03:04

5 Answers5

96

read.csv() is a wrapper around the more general read.table() function. That latter function has argument check.names which is documented as:

check.names: logical.  If ‘TRUE’ then the names of the variables in the
         data frame are checked to ensure that they are syntactically
         valid variable names.  If necessary they are adjusted (by
         ‘make.names’) so that they are, and also to ensure that there
         are no duplicates.

If your header contains labels that are not syntactically valid then make.names() will replace them with a valid name, based upon the invalid name, removing invalid characters and possibly prepending X:

R> make.names("$Foo")
[1] "X.Foo"

This is documented in ?make.names:

Details:

    A syntactically valid name consists of letters, numbers and the
    dot or underline characters and starts with a letter or the dot
    not followed by a number.  Names such as ‘".2way"’ are not valid,
    and neither are the reserved words.

    The definition of a _letter_ depends on the current locale, but
    only ASCII digits are considered to be digits.

    The character ‘"X"’ is prepended if necessary.  All invalid
    characters are translated to ‘"."’.  A missing value is translated
    to ‘"NA"’.  Names which match R keywords have a dot appended to
    them.  Duplicated values are altered by ‘make.unique’.

The behaviour you are seeing is entirely consistent with the documented way read.table() loads in your data. That would suggest that you have syntactically invalid labels in the header row of your CSV file. Note the point above from ?make.names that what is a letter depends on the locale of your system; The CSV file might include a valid character that your text editor will display but if R is not running in the same locale that character may not be valid there, for example?

I would look at the CSV file and identify any non-ASCII characters in the header line; there are possibly non-visible characters (or escape sequences; \t?) in the header row also. A lot may be going on between reading in the file with the non-valid names and displaying it in the console which might be masking the non-valid characters, so don't take the fact that it doesn't show anything wrong without check.names as indicating that the file is OK.

Posting the output of sessionInfo() would also be useful.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • Nice answer Gavin +1 I wonder if there's a space in front of the header name as `make.names(" Foo")` creates `"X.Foo"` as well. – Tyler Rinker May 04 '12 at 15:18
  • Is it possible to use a different character instead of `X`? – Dan Sep 14 '16 at 02:59
  • 10
    A very simplified summary of this perfect answer would be, try adding this to your read.table() if you don't want R to change your names: `check.names=FALSE` – FatihSarigol Oct 07 '18 at 12:28
14

I just came across this problem and it was for a simple reason. I had labels that began with a number, and R was adding an X in front of them all. I think R is confused with a number in the header and applies a letter to differentiate from values.

So, "3_in" became "X3_in" etc... I solved by switching the label to "in_3" and the issues was resolved.

I hope this helps someone.

Matt Beam
  • 141
  • 1
  • 2
12

When the column names don´t have correct form, R put an "X" at the start of the column name during the import. For example it is usually happening when your column names starts with number or some spacial character. The check.names = FALSE cause it will not happen - there will be no "X". However some functions may not work if the column names starts with numbers or other special character. Example is rbind.fill function.

So after the application of that function (with "corrected colnames") I use this simple thing to get rid of the "X".

destroyX = function(es) {
  f = es
  for (col in c(1:ncol(f))){ #for each column in dataframe
    if (startsWith(colnames(f)[col], "X") == TRUE)  { #if starts with 'X' ..
      colnames(f)[col] <- substr(colnames(f)[col], 2, 100) #get rid of it
    }
  }
  assign(deparse(substitute(es)), f, inherits = TRUE) #assign corrected data to original name
}
Dorregaray
  • 381
  • 3
  • 6
6

I ran over a similar problem and wanted to share the following lines of code to correct the column names. Certainly not perfect, since clean programming in the forehand would be better, but maybe helpful as a starting point to someone as quick and dirty approach. (I would have liked to add them as comment to Ryan's question/Gavin's answer, but my reputation is not high enough, so I had to post an additional answer - sorry).

In my case several steps of writing and reading data produced one or more columns named "X", X.1",... containing content in the X-column and row numbers in the X.1,...-columns. In my case the content of the X-column should be used as row names and the other X.1,...-columns should be deleted.

Correct_Colnames <- function(df) {

 delete.columns <- grep("(^X$)|(^X\\.)(\\d+)($)", colnames(df), perl=T)

  if (length(delete.columns) > 0) {

   row.names(df) <- as.character(df[, grep("^X$", colnames(df))])
   #other data types might apply than character or 
   #introduction of a new separate column might be suitable

   df <- df[,-delete.columns]

   colnames(df) <- gsub("^X", "",  colnames(df))
   #X might be replaced by different characters, instead of being deleted
  }

  return(df)
}
Manuel Bickel
  • 2,156
  • 2
  • 11
  • 22
5

I solved a similar problem by including row.names=FALSE as an argument in the write.csv function. write.csv was including the row names as an unnamed column in the CSV file and read.csv was naming that column 'X' when it read the CSV file.

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Tristan
  • 51
  • 1
  • 3
  • This answer deserves more upvotes! I did not have weird symbols nor numbers in my column names but still have this issue of an "X" column created when reading csv. Your solution solved the problem perfectly! It was the row names (that I am not aware was there). – RachelSunny May 27 '22 at 12:09