R - column names in read.table and write.table, starting from the number and containing the space

I import csv of stock data into R, with column names of a stock ticker that starts with a number and contains space inside, for example. "5560 JP". After reading in R, column names are added with "X", and the space is replaced with ".", For example. "X5560.JP". After all the work is done in R, I want to write the processed data back to the new csv, but with the original column name, for example. "5560 JP" instead of "X5560.JP", how can I do this?

Thanks!

+6
source share
1 answer

When you use write.csv or write.table to save your data in a CSV file, you can set the column names to whatever you like by setting the col.names argument.

But it is assumed that column names are available. After you read the data and R changed the names, you lost this information. To get around this, you can suppress the conversion to get the column names:

 df <- read.csv("mydata.csv", check.names=FALSE) orig.cols <- colnames(df) colnames(df) <- make.names(colnames(df)) [your original code] write.csv(df, col.names=orig.cols) 
+19
source

Source: https://habr.com/ru/post/921050/


All Articles