write.fwf列名不与值对齐

时间:2017-08-15 03:48:00

标签: r io gams-math

以下代码生成一个表,其列名不与其值对齐:

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) 

如何使列名与每列的值对齐(为了使表能够被GAMS读取)?

1 个答案:

答案 0 :(得分:1)

很奇怪列名称不会以相同的方式处理。解决方法可能是将列名添加为表中的一行,然后编写没有列名的表...

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 )

这看起来像:

enter image description here