write.table writes unwanted leading empty column to header when has rownames

前端 未结 5 1183
-上瘾入骨i
-上瘾入骨i 2020-12-07 09:22

check this example:

> a = matrix(1:9, nrow = 3, ncol = 3, dimnames = list(LETTERS[1:3], LETTERS[1:3]))
> a
  A B C
A 1 4 7
B 2 5 8
C 3 6 9
相关标签:
5条回答
  • 2020-12-07 09:40

    For anyone working in the tidyverse (dplyr, etc.), the rownames_to_column() function from the tibble package can be used to easily convert row.names to a column, e.g.:

    library('tibble')
    a = as.data.frame(matrix(1:9, nrow=3, ncol=3, 
                      dimnames=list(LETTERS[1:3], LETTERS[1:3])))
    
    a %>% rownames_to_column('my_id')
    
      my_id A B C
    1     A 1 4 7
    2     B 2 5 8
    3     C 3 6 9
    

    Combining this with the row.names=FALSE option in write.table() results in output with header names for all columns.

    0 讨论(0)
  • 2020-12-07 09:44

    A slight modification to @Marek very helpful answer WILL add a header to the rownames column: temporarily add the rownames as the first column in the data.frame, and write that, ignoring the real rownames.

    > a = matrix(1:9, nrow = 3, ncol = 3, dimnames = list(LETTERS[1:3], LETTERS[1:3]))
    > write.table(data.frame("H"=rownames(a),a),"a.txt", row.names=FALSE)
    

    and you get

    "H" "A" "B" "C"
    "A" 1 4 7
    "B" 2 5 8
    "C" 3 6 9
    
    0 讨论(0)
  • 2020-12-07 09:45

    I revised a simple function from @mnel, which adds flexibility by using connections. Here is the function:

    my.write <- function(x, file, header, f = write.csv, ...){
    # create and open the file connection
    datafile <- file(file, open = 'wt')
    # close on exit 
    on.exit(close(datafile))
    # if a header is defined, write it to the file (@CarlWitthoft's suggestion)
    if(!missing(header)) {
    writeLines(header,con=datafile, sep='\t')
    writeLines('', con=datafile, sep='\n')
    }
    # write the file using the defined function and required addition arguments  
    f(x, datafile,...)
    }
    

    You can specify the function to be 'write.table', 'write.csv', 'write.delim' etc.

    Cheers!

    0 讨论(0)
  • 2020-12-07 09:47

    Citing ?write.table, section CSV files:

    By default there is no column name for a column of row names. If col.names = NA and row.names = TRUE a blank column name is added, which is the convention used for CSV files to be read by spreadsheets.

    So you must do

    write.table(a, 'a.txt', col.names=NA)
    

    and you get

    "" "A" "B" "C"
    "A" 1 4 7
    "B" 2 5 8
    "C" 3 6 9
    
    0 讨论(0)
  • 2020-12-07 09:49

    For those who experience the same issue when saving a matrix with write.table() and want to keep the row.names column, there is actually an extremely simple solution:

     write.table(matrix,file="file.csv",quote=F,sep=";", row.names=T
                 col.names=c("row_name_col;val1_col","val2_col"))
    

    By doing that you're basically tricking the write.table function into creating a header label for the row.names column. The resulting .csv file would look like this:

    row_name_col;val1_col;val2_col
    row1;1;4 
    row2;2;5 
    row3;3;6 
    
    0 讨论(0)
提交回复
热议问题