如何在不取消引用其他数据的情况下取消引用标题?

时间:2017-03-29 18:47:35

标签: r

我的数据如下所示:

Scenario,ScenName,Step,Date,GBP_fx,EUR_fx
0,"assets",0,"30/09/2016",1,0.865126741

这是一个包含100k观测值的巨大文件。我在R中读取它并将其修剪为10k然后将其写入csv。但是,我找不到如何在不丢失数据中的引号的情况下仅取消引用标题。如果我保留quote = TRUE,这就是最终数据的样子,但我想摆脱标题中的引号。

"Scenario","ScenName","Step","Date","GBP_fx","EUR_fx"
0,"assets",0,"30/09/2016",1,0.865126741

有什么建议吗?

3 个答案:

答案 0 :(得分:5)

您可以编写标题,然后附加行,例如:

# input sample
testDF <- read.csv(text=
'Scenario,ScenName,Step,Date,GBP_fx,EUR_fx
0,"assets",0,"30/09/2016",1,0.865126741')

# custom write.csv function
write.csv.noheaderquote <- function(x,file){
  # write header
  write.table(head(x,0),file=file,sep=',',quote=FALSE,row.names = FALSE)
  # append the rest of the rows
  write.table(x,file=file,sep=',',row.names=FALSE,append=TRUE,col.names=FALSE)
}

# use the new function to write your csv
write.csv.noheaderquote(testDF,"destfile.csv")

结果destfile.csv

Scenario,ScenName,Step,Date,GBP_fx,EUR_fx
0,"assets",0,"30/09/2016",1,0.865126741

答案 1 :(得分:0)

我不确定一切都是必要的,但是如果你的输入保存在csv&#34; mycsv.csv&#34;中,你就可以了,包括stringr

library(stringr)
df = read.table("mycsv.csv", sep = ",", quote = "", header = TRUE)
colnames(df) = str_replace( str_replace(colnames(df), "X.", ""), "\\.", "" )

答案 2 :(得分:0)

看起来参数引用本身会这样做。使用quote="",如:

testDF <- read.csv(text=
'Scenario,ScenName,Step,Date,GBP_fx,EUR_fx
0,"assets",0,"30/09/2016",1,0.865126741', quote="")
一个人得到:

> testDF
  Scenario ScenName Step         Date GBP_fx    EUR_fx
1        0 "assets"    0 "30/09/2016"      1 0.8651267