riogams-math

write.fwf column names don't line up with values


The following code produces a table whose column names don't line up with its values:

library( gdata )
test0 <- matrix(5:28, nrow = 4) 
row.names(test0) <- paste("r", 1:4, sep = "")
colnames(test0) <- paste("c", 1:6, sep = "")

test0[3, 2] <- 1234567890
test0[ , 3] <- 0.19412341293479123840214

test0 <- format(test0, digits = 5, trim = T, width = 10, scientific = T)

write.fwf(test0, file = paste("test", ".txt", sep = ""), width = 11, rowname = T, colname = T, quote = F) 

How can I make column names line up with each column's values (in order to have the table to be readable by GAMS)?


Solution

  • Curious that the column names are not treated in the same way. A work-around could be to add the column names as a row in the table and then write the table without column names...

    i.e.

    test1 <- rbind( colnames(test0) , test0 )
    write.fwf(test1, file = paste("test", ".txt", sep = ""),
              width = 11,
              rownames = T,
              colnames = F,  #Don't print the column names
              quote = F )
    

    This looks like:

    enter image description here