zeno tsang zeno tsang - 2 months ago 7
R Question

How to replace the "." in column names generated by read.csv() with a single space when exporting?

I am using R to do some data pre-processing, and here is the problem that I am faced with: I input the data using

read.csv(filename,header=TRUE)
, and then the space in variable names became ".", for example, a variable named Full Code became Full.Code in the generated dataframe. After the processing, I use
write.xlsx(filename)
to export the results, while the variable names are changed. How to address this problem?

Besides, in the output .xlsx file, the first column become indices(i.e., 1 to N), which is not what I am expecting.

Answer

If your set check.names=FALSE in read.csv when you read the data in then the names will not be changed and you will not need to edit them before writing the data back out. This of course means that you would need quote the column names (back quotes in some cases) or refer to the columns by location rather than name while editing.