在r函数中写入csv

在r函数中写入csv,r,csv,R,Csv,我正在尝试编写一个函数,将输出写入该函数中的csv文件。我试图将csv文件定向到工作目录中标记为“test”的文件夹。当我运行这个函数然后使用它时,什么都没有发生 这就是功能: SCTble <- function(dat, datname) { y<-ddply(dat, .(Species, Country), summarize, Pounds = round(sum(Pounds), 0 ), Dollars = round(sum(Dollars),0), UnitVal

我正在尝试编写一个函数,将输出写入该函数中的csv文件。我试图将csv文件定向到工作目录中标记为“test”的文件夹。当我运行这个函数然后使用它时,什么都没有发生

这就是功能:

SCTble <- function(dat, datname) {
y<-ddply(dat, .(Species, Country), summarize, Pounds = 
round(sum(Pounds), 0 ), Dollars = round(sum(Dollars),0), UnitValue = 
round((Dollars/Pounds),2))
y<-y%>%
mutate('Share Volume' = round(Pounds/sum(Pounds)*100,2))%>%
mutate('Share Value'= round(Dollars/sum(Dollars)*100,2))
datname<-as.data.frame(y)
return(datname)
write.csv(y, paste("test/",datname, ".csv"), row.names= TRUE)
  }

SCTble您在
返回
之后获得了
write.csv
,因此该函数永远不会到达
write.csv
<代码>返回
退出函数。谢谢,@Spacedman。我在write.csv之后移动了返回值,但现在我得到了以下错误:
文件中的错误(file,ifelse(append,“a”,“w”)):除此之外的“description”参数无效:警告消息:在if(file==“”)文件1中,只有第一个元素将从以下位置调用:file(file,ifelse(append,“a”,“w”))
您已经用此行覆盖了传入的
datname
datname@Spacedman非常感谢。我去掉了那条线,现在它工作得很好。