Search code examples
rdataframeread.csv

R read.csv Importing Column Names Incorrectly


I have a csv that I would like to import into R as a data.frame. This csv has headers such as USD.ZeroCouponBondPrice(1m) and USD-EQ-SP500 that I can't change. When I try to import it into R, however, R's read.csv function overwrites the characters ()- as . Although I wasn't able to find a way to fix this in the function documentation, this line of code worked:

colnames(df)<-c('USD.ZeroCouponBondPrice(1m)', 'USD-EQ-SP500')

so those characters are legal in data.frame column names. Overwriting all of the column names is annoying and fragile as there are over 20 of them and it is not unthinkable for them to change. Is there a way to prevent read.csv from replacing those characters, or an alternative function to use?


Solution

  • If you set the argument

    check.names = FALSE
    

    in read.csv, then R will not override the names. But these names are not valid in R and they'll have to be handled differently than valid names.