【问题标题】:Writing data frame to semicolon-delimited CSV where column data contains semi-colon?将数据框写入以分号分隔的 CSV,其中列数据包含分号?
【发布时间】:2016-02-01 07:16:11
【问题描述】:

如何在以下数据完整的情况下写入 CSV?我想将此数据写入 CSV 文件,分隔符为分号,但由于分号,第二行正在移动。

我有一个包含以下数据的数据框:

             a <- c(1,2,3,4)
             b <- c("hello","hello;world","hey","there")
             c <- c(10,20,30,40)
             df <- data.frame(a,b,c)
             df
             df
               a           b  c
             1 1       hello 10
             2 2 hello;world 20
             3 3         hey 30
             4 4       there 40

【问题讨论】:

  • (“CSV 文件”一般指任何以分隔符分隔的文本文件,字段包含该分隔符是合法的,这就是引用的用途)

标签: r csv dataframe separator quoting


【解决方案1】:

引用输出:

a<-c(1,2,3,4)
b<-c("hello","hello;world","hey","there")
c<-c(10,20,30,40)
df<-data.frame(a,b,c)
write.table(df, file="blah.csv",quote=TRUE, sep = ";")

给予

"a";"b";"c"
"1";1;"hello";10
"2";2;"hello;world";20
"3";3;"hey";30
"4";4;"there";40

【讨论】:

  • OP注意:quote=TRUEwrite.table的默认值,所以你不必每次使用write.table时都指定它。
  • 我有一个问题。通过添加quote="TRUE",列名也将被引用。可以以任何方式防止这种情况吗?我只想要不带引号的列名的上述行为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多