1

I have code for a loop that seems to mostly work, but when I try to output the results into a csv file, I get the following error, Error in file(file, ifelse(append, "a", "w")) : invalid 'open' argument.

I've written code for a loop (which has worked before) that takes a dataset from a folder, performs calculations and puts those calculations in new columns, and then outputs that dataset with the new columns into a different folder. However, R encounters a problem when trying to put the output into the new folder at the end of the loop, giving me the following error, Error in file(file, ifelse(append, "a", "w")) : invalid 'open' argument.

setwd("/Users/Desktop/Snail/CSVs/CSV 2")


files = list.files()

summary = NULL

for (f in files) {
  data = read.csv(f, sep = ',',header=T)
  data2<-prepData(data,type="UTM",coordNames=c("x","y"))
  sub = cbind(f,data2)
  summary = rbind(summary,sub)
  myfile = file.path("/Users/Desktop/CSV 3",".csv")
  write.table(summary[[f]], file=myfile , paste(names(summary)[f],".csv",sep=","))
}

I am hoping to get a file with the new calculations in the new folder.

Rana
  • 25
  • 5
  • 1
    Why are you assigning `file=myfile` and then apparently assigning another filename with `paste(names(summary)...)`? That is going into the third argument of `write.table`, which is `append=` looking for a logical argument. – r2evans Apr 08 '19 at 17:30
  • I tried rewriting it as write.table(summary[[f]], file="/Users/carlcloyed/Desktop/CSV 3",paste(names(summary)[f]),".csv" ,".csv",sep=",") But get Error in write.table(summary[[f]], file = "/Users/carlcloyed/Desktop/CSV 3", : invalid 'quote' specification – Rana Apr 08 '19 at 17:40
  • You missed my point. You are trying to pass two different file names to the function! The second filename is being passed into the `append=` argument of `write.table`, which is causing your error. Just try `write.table(summary[[f]], file=myfile)`. – r2evans Apr 08 '19 at 17:42
  • With myfile I was trying to set a path to where I want the files to go and then tried to specify the name of the file in write.table with paste(names(summary)[f] I rewrote myfile = file.path("/Users/Desktop/CSV3",paste(names(summary)[f],".csv")) And now get Error in match.names(clabs, names(xi)) : names do not match previous names I really appreciate your help! – Rana Apr 08 '19 at 17:58
  • The two hardest things in computer science: naming variables, and cache invalidation. I suggest your use of `myfile` as a name for a *directory* is a great example of violating the first one. – r2evans Apr 08 '19 at 18:10
  • Your `match.names` error is coming from `rbind`, which means your data does not look like you think it does. My guess is that there is a column in `summary` that is not in `sub` or vice versa. (However, we cannot know with certainty with the question as written.) – r2evans Apr 08 '19 at 18:13
  • Did the `rbind` comment resolve that final issue? – r2evans Apr 08 '19 at 20:23

1 Answers1

0

Your call to write.table is explicitly passing what I believe should be your filename into the append= argument. Try this:

for (f in files) {
  data = read.csv(f, sep = ',',header=T)
  data2<-prepData(data,type="UTM",coordNames=c("x","y"))
  sub = cbind(f,data2)
  summary = rbind(summary,sub)
  myfile = file.path("/Users/Desktop/CSV 3", paste(names(summary)[f],".csv",sep=","))
  write.table(summary[[f]], file=myfile)
}
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Oh excellent, thanks for your time and help! I went ahead and did this but now get a new error Error in match.names(clabs, names(xi)) : names do not match previous names – Rana Apr 08 '19 at 18:05
  • That new error from `match.names` is most likely a duplicate of https://stackoverflow.com/q/12019461/3358272 – r2evans Apr 08 '19 at 21:05
  • Rana, does this answer your question? If not, please provide more context/details. (If so, please accept it :-) – r2evans Apr 23 '19 at 00:17