【发布时间】:2023-03-14 17:30:02
【问题描述】:
我有一个 2 x 2 csv 例如:
A,B
1.12,1.24
如果我将它读入R,然后不做任何事情覆盖旧文件,它现在将在终端中打印为:
"A","B"
1.12,1.24
如何阻止这种情况发生? R 代码:
dat <- read.csv(filePath,header=FALSE,sep=",",skip=1)
colNames <- strsplit(readLines(filePath,1),",")[[1]]
colnames(dat) <- colNames
write.csv(dat,filePath,row.names=FALSE)
请注意,上述 MWE 中的 dat 是 data.frame。
【问题讨论】:
-
cat(dat)- 引号实际上并不存在。它们表示数据的类型为"character"。 -
您能否解释一下为什么使用
header=FALSE,然后再使用colnames添加标题。还要注意write.csv中的quote参数。