[R] Naming output file

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Mon Jun 24 13:57:28 CEST 2024


В Mon, 24 Jun 2024 19:41:10 +0800
Steven Yen <styen using 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



More information about the R-help mailing list