如果在包含大量write.table
符号的输入上使用\t
,如何避免> x <- c("1\t119\t120\t1\t119\t120\tABC\tDEF\t0", "2\t558\t559\t2\t558\t559\tGHI\tJKL\t0", "3\t139\t141\t3\t139\t141\tMNO\tPQR\t0", "3\t139\t143\t3\t139\t143\tSTU\tVWX\t0")
> x
[1] "1\t119\t120\t1\t119\t120\tABC\tDEF\t0"
[2] "2\t558\t559\t2\t558\t559\tGHI\tJKL\t0"
[3] "3\t139\t141\t3\t139\t141\tMNO\tPQR\t0"
[4] "3\t139\t143\t3\t139\t143\tSTU\tVWX\t0"
> write.table(x, file = "file.txt", row.names = FALSE, col.names = c("A", "B", "C", "D", "E", "F", "G", "H", "I"))
Error in write.table(x, file = "uuu.txt", row.names = FALSE, col.names = c("A", :
invalid 'col.names' specification
错误?示例代码:
> write.table(x, file = "uuu.txt", row.names = FALSE, col.names = FALSE)
以下命令确实生成了一个输出表,但仍然保留了双方的双引号:
"1 119 120 1 119 120 ABC DEF 0"
"2 558 559 2 558 559 GHI JKL 0"
"3 139 141 3 139 141 MNO PQR 0"
"3 139 143 3 139 143 STU VWX 0"
输出:
A B C D E F G H I
1 119 120 1 119 120 ABC DEF 0
2 558 559 2 558 559 GHI JKL 0
3 139 141 3 139 141 MNO PQR 0
3 139 143 3 139 143 STU VWX 0
相反,我想看看:
strsplit(x, split = "\t")
我尝试了write.table
的某些组合来在调用invalid 'col.names' specification
之前预处理输入,但遇到了同样的错误:{{1}}。
答案 0 :(得分:2)
如果数据不是以表格形式存在,则无法将数据写入表格形式。读入x
,然后用
write.table(read.table(text = x), 'file.txt', col.names = LETTERS[1:9], quote = FALSE)