【问题标题】:Saving R Dataframe as filename based on objects将 R Dataframe 保存为基于对象的文件名
【发布时间】:2018-06-08 18:44:57
【问题描述】:

我在 R 中有一些对象会不时改变:

    beginning <- "Our_Office_Preface"
    query_type <- "PA_"
    date <- "2018_06_08_"
    office_query_type <- "dis_"
    input_FY <- 2015
    filename <- paste(beginning,"",query_type,"",date,"",office_query_type,"",input_FY,sep="")

我正在尝试将数据框编写为制表符分隔的 txt 文件。我正在尝试将其保存为 filename 并且很难将其设为制表符分隔。

df <- data.frame(name=c('Inst1','Inst2','Inst3','Inst4','Inst5','Inst6','Inst7','Inst8','Inst9','Inst10'), num=c(1,5,6,7,4,6,5,7,8,4))

这两行

write.table(df , file=paste(filename, ".txt", sep="\t", quote = FALSE))
write.table(df , file=paste(filename, ".txt", sep="\t"), append = FALSE, quote = FALSE, row.names = FALSE, col.names = TRUE)

两者都产生了这个错误

Error in file(file, ifelse(append, "a", "w")) : cannot open the connection

我能得到一些帮助吗?

【问题讨论】:

    标签: r


    【解决方案1】:

    您只是在 paste 函数上放错了右括号。 sep = "\t"write.table 参数,它被传递给 paste

    ".txt" 之后关闭paste 函数,它应该可以工作。

    write.table(df , file=paste(filename, ".txt"), sep="\t", quote = FALSE)
    

    【讨论】:

    • 糟了!我知道它会是那样的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多