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]]
Hi, try with: m<-3 for (im in 1:m) { mydata<-read.csv(paste0("2010midata",im,".csv")) assign(paste0("bop",im),boprobit(eqs,mydata,wt=weight,method="BHHH",tol=0,reltol=0,gradtol=1e-5, Fisher=TRUE) } cheers F. On 6/24/24 06:41, Steven Yen 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 at 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.
How about assign(paste0("bop",im), boprobit( etc )) On Mon, Jun 24, 2024 at 2:41?PM Steven Yen <styen at 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 at 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. >[[alternative HTML version deleted]]
?s 12:41 de 24/06/2024, Steven Yen escreveu:> 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 at 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.Hello, Here are two ways, with a for loop and with a lapply loop. # for loop m <- 3 # create the input filenames in one instruction INPUT <- paste0("2010midata", seq.int(m), ".csv") # create a named list with m elements to store the output OUTPUT <- vector("list", length = m) |> setNames(paste0("bop", seq.int(m))) for(i in seq.int(m)) { mydata <- read.csv(INPUT[[i]]) OUTPUT[[i]] <- boprobit(eqs, mydata, wt=weight, method="BHHH", tol=0, reltol=0, gradtol=1e-5, Fisher=TRUE) } # lapply loop m <- 3 # create the input filenames in one instruction INPUT <- paste0("2010midata", seq.int(m), ".csv") # no need to create the output list, it will be the # return value of lapply OUTPUT <- lapply(INPUT, \(f) { mydata <- read.csv(f) boprobit(eqs, mydata, wt=weight, method="BHHH", tol=0, reltol=0, gradtol=1e-5, Fisher=TRUE) }) # assign the output list's names names(OUTPUT) <- paste0("bop", seq.int(m)) Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
? Mon, 24 Jun 2024 19:41:10 +0800 Steven Yen <styen at ntu.edu.tw> ?????:> (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.If you really want to generate variable names and assign then in the global environment programmatically, you can use assign(paste0("bop", im), boprobit(...)), but I wouldn't recommend to do this. When there's a thousand of these files, would you really want to have 1000 variables and access them in a similarly awkward fashion using get(variablename)? Your analysis results are clearly related and form a sequence. Why not put them in a list? bop <- list() # or: be a good citizen and preallocate the list bop <- vector('list', 3) # instead of assign(...): bop[[im]] <- boprobit(...) This way you can still address the individual elements of the sequence using bop[[1]], bop[[2]], bop[[3]], but you can also operate on the whole sequence, e.g., save it to a file using saveRDS(bop, 'bop.rds'). Lists also compose better with lapply(), parLapply(), and many other functions that operate on collections of R objects. -- Best regards, Ivan
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 <styen at 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 at 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.