【问题标题】:Remove quotation marks around header in the output from write.csv删除 write.csv 输出中标头周围的引号
【发布时间】: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 中的 datdata.frame

【问题讨论】:

  • cat(dat) - 引号实际上并不存在。它们表示数据的类型为"character"
  • 您能否解释一下为什么使用header=FALSE,然后再使用colnames 添加标题。还要注意write.csv 中的quote 参数。

标签: string r csv io header


【解决方案1】:

试试这个:

df <- read.csv(text = "A,B
1.12,1.24")

df
#      A    B
# 1 1.12 1.24

write.csv(x = df, file = "df.txt", quote = FALSE)

df.txt 在记事本中如下所示:

,A,B
1,1.12,1.24

【讨论】:

    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    相关资源
    最近更新 更多