gpt4 book ai didi

r - 当有行名时,write.table 将不需要的前导空列写入标题

转载 作者:行者123 更新时间:2023-12-03 05:06:53 24 4
gpt4 key购买 nike

检查这个例子:

> 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

表格显示正确。有两种不同的方式将其写入文件...

write.csv(a, 'a.csv') 按预期给出:

"","A","B","C"
"A",1,4,7
"B",2,5,8
"C",3,6,9

write.table(a, 'a.txt') 搞砸了

"A" "B" "C"
"A" 1 4 7
"B" 2 5 8
"C" 3 6 9

确实,缺少一个空选项卡......这对于下游事物来说是一个痛苦的事情。这是一个错误还是一个功能?有解决方法吗? (除了 write.table(cbind(rownames(a), a), 'a.txt', row.names=FALSE)

干杯,扬尼克

最佳答案

引用 ?write.tableCSV 文件部分:

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.

所以你必须这样做

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

你会得到

"" "A" "B" "C"
"A" 1 4 7
"B" 2 5 8
"C" 3 6 9

关于r - 当有行名时,write.table 将不需要的前导空列写入标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2478352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com