I had the same FIRST impression which could have made sense as in writing the data back out to a file with a similar name and that would have been trivial. I mean if you had exactly three, a loop would not even be particularly useful versus writing two lines, copying them and editing the latter two sets.
But the OP could have done a better job explaining what they wanted it all FOR. Just writing out the same file contents would be nonsensical unless some change was made such as writing in a different format. But I noted the output names were not something like NAME.csv and although the code looked wrong, it soon became clear they wanted to create not files but clearly a data.frame as that is produced by read.csv. The language usage was simply sloppy. The names chosen made it easier as all you needed to do was replace "2010midata" with "bop" and easy enough to do in a loop or several other ways but what the OP missed was how to do something that is doable in an interpreted language like R but possibly not in many compiled languages. So, as others pointed out, sometimes the person doing this has become wedded to ONE WAY of doing something rather than considering if some other way is a better choice. In this case, a consensus is that a data structure such as a list of data.frames would be a better choice and even that it could be done without an explicit loop such as this version where you can set N to any value, and it will get files with a suffix ranging from 1 to N, albeit this does not allow you to find something like 007 without modification: N <- 3 list_of_df <- lapply(paste0("2010midata", 1:N, ".csv"), read.csv) An individual data.frame can be accessed using [[]] notation as in: > class(list_of_df[[1]]) [1] "data.frame" Of course if you have additional values to pass in to read.csv, add them in the ... after the first args and so on. The next part, badly written as: paste0("bop",im)<-boprobit(eqs,mydata,wt=weight,method="BHHH",tol=0,reltol=0 ,gradtol=1e-5,Fisher=TRUE) Not can use a similar construction in which the 1:N is replaced by list_of_df and the function by boprobit and then the remaining arguments in the ... But that is not the way many initially think. Let alone thinking in nested fashion as in: Results <- lapply(X=lapply(X=paste0("2010midata", 1:N, ".csv"), FUN=read.csv), FUN=boprobit, ,wt=weight,method="BHHH",tol=0,reltol=0,gradtol=1e-5,Fisher=TRUE)) Unfortunately, the latter is WRONG as boprobit seems to take a second argument of the data.frame with whatever "eqs" is as the first argument. This slightly nonstandard version would require some gimmick such as an accessory function that rearranges what it gets as arguments and then calls boprobit. But, as noted, this is one of many examples where the user comes up with a "solution" that is fairly rarely needed even if it can be done, rather than one that is a bit more abstract and yet powerful. Yet, as noted, R can do things we often have no need for and this is an example. Never mind how it can be a tad dangerous to assign to extra variable names that might step on existing names as compared to creating anonymous data structures with few or no names as everything is largely positional. I will end with a slight variant. Imagine making a named list, perhaps extended in each part of the loop used, so that you it looks like Mylist <- list(name1=value, name2=value, name3=value) This could then be addressed using Mylist[[1]] or Mylist$name1 interchangeably. But as all names would be internal to the list, it is name-safe. -----Original Message----- From: R-help <r-help-boun...@r-project.org> On Behalf Of Richard O'Keefe Sent: Monday, June 24, 2024 9:05 PM To: Steven Yen <st...@ntu.edu.tw> Cc: R-help Mailing List <r-help@r-project.org>; Steven Yen <sye...@gmail.com> Subject: Re: [R] Naming output file The subject line says (capitalisation changed) "name output FILE" but I see no attempt in the sample code to create an output FILE. I was expecting to see something like write(<whatever>, file = paste0("bop",im)) On Mon, 24 Jun 2024 at 23:41, Steven Yen <st...@ntu.edu.tw> wrote: > > I would like a loop to > > (1) read data files 2010midata1,2010midata2,2010midata3; and > > (2) name OUTPUT bop1,bop2,bop3. > > I succeeded in line 3 of the code below, > > BUT not line 4. The error message says: > > Error in paste0("bop", im) <- boprobit(eqs, mydata, wt = weight, method > = "NR", : target of assignment expands to non-language object Please > help. Thanks. > > m<-3 > for (im in 1:m) { > mydata<-read.csv(paste0("2010midata",im,".csv")) > paste0("bop",im)<-boprobit(eqs,mydata,wt=weight,method="BHHH",tol=0,reltol=0 ,gradtol=1e-5,Fisher=TRUE) > } > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.