read.csv replaces column-name characters like `?` with `.`, `-` with `...`

Will picture Will · Dec 24, 2014 · Viewed 7.5k times · Source

I'm using RStudio and my output on the Console gets truncated. I can't find how to stop the truncation (I tried searching ?options as well as googling around for longer than I'd like to admit).

EDIT: My apologies everyone! I originally had the long name as 'ThisIsAReallyReallyReallyReallyReallyLongName', but the issue only came up with the long name of 'Translation Service Info - Which translation service?'. I think I found the issue. The ... wasn't truncating, it was replacing the unknown characters like ? and - with . and ....

Code

# Load File
myfile <- read.csv(file="C:\\Users\\wliu\\Desktop\\myfile.csv",
               sep=",", header=TRUE, stringsAsFactors=FALSE, skip=2)

# Get my column names
mycolnames <- colnames(myfile)

# When I request a shorter name, this returns the full name
mycolnames[1]  # Assuming first col is a short name
[1] "ThisIsAShortName"

# However, when I request a longer name, this returns a truncated version
mycolnames[2]  # Assuming second col is a really long name
[1] "ThisIsA...Long...Name"

I want to get back the non-truncated version of mycolnames[2] (e.g. "ThisIsAReallyReallyReallyReallyReallyLongName")

Setup

I'm on Windows 7 64bit, RStudio Version 0.98.1091, R version 3.0.1 (2013-05-16) -- "Good Sport" with Platform: x86_64-w64-mingw32/x64 (64-bit). I tried with 'Use Git Bash as shell for Git projects' on and off.

myfile.csv

ThisIsAShortName, Translation Service Info - Which translation service?

23143505, Yes
23143614, No
23143324, Yes

Char Replacement instead of truncating issue

Answer

Rich Scriven picture Rich Scriven · Dec 24, 2014

This is expected behavior by read.csv, not a truncation problem in R. When you have spaces and special characters in the column names of a file, read.csv replaces each of them with a . unless you specify check.names = FALSE

Here's a glimpse at make.names, which is how read.table produces the column names.

nm <- "Translation Service Info - Which translation service?"
make.names(nm)
# [1] "Translation.Service.Info...Which.translation.service."

And here's the relevant line from read.table

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