R - column names in read.table and write.table starting with number and containing space

后端 未结 1 1034
走了就别回头了
走了就别回头了 2020-12-18 01:58

I am importing a csv of stock data into R, with column names of stock ticker which starts with number and containing space inside, e.g. \"5560 JP\". After reading into R, th

相关标签:
1条回答
  • 2020-12-18 02:16

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

    But that assumes you have the column names to available. Once you've read in the data and R has converted the names, you've lost that 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)
    
    0 讨论(0)
提交回复
热议问题