0

If I have this list here:

list1 <- list(2, "hello", c(3, 5, 4))

and I have this:

ff <- "Bar"

I want to change the name of the list from "list1" to "Bar" via ff.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Tpellirn
  • 660
  • 4
  • 11
  • 3
    What’s the purpose of this? Is is generally a **very bad idea** to change variable names dynamically. For all intents and purposes, you should consider variable names to be *static*, and tied to the code you write. In fact, most languages don’t allow this at all, and for good reason. – Konrad Rudolph Jan 05 '22 at 10:10
  • 1
    You should use (nested) lists for this purpose then. Then you can add new elements to the list inside the loop. Better yet would be not to use loops but instead generate the list with a function such as `lapply`. If you can show the actual data you’re working with, we can give you more concrete help. – Konrad Rudolph Jan 05 '22 at 10:21
  • One last comment: you can use either `<-` or `=` for assignment, both work and are [identical](https://stackoverflow.com/a/51564252/1968) here. But stick to one, don’t mix and match them throughout your code. – Konrad Rudolph Jan 05 '22 at 10:30
  • As suggested by @Konrad Rudolph usually you prefer one of the apply functions, for cases where you want a named list you probably want to use sapply over lapply, so that your list items are named accordingly to the values you apply over. Small example: `sapply(c("Bar", "SAV"), FUN = function(x) "do something", simplify = FALSE, USE.NAMES = TRUE)` – Merijn van Tilborg Jan 05 '22 at 12:18

2 Answers2

2

You’d use (nested) lists for this purpose. At its most rudimentary, this could look as follows:

results = list()

for (var in c('foo', 'bar', 'baz')) {
    results[[var]] = list1
}

This will generate a new list with three items named foo, bar and baz, each of which is a copy of list1. To access the individual values, you’d use e.g. result$foo, or result[['foo']] (the latter is necessary if you have the value’s name stored in a variable, since access via $ doesn’t work with variables on the right-hand side).

You can adapt this for your specific purposes. Do note that, in most cases, the loop would be replaced by a more appropriate function. For instance, the above would be done better using replicate:

result = setNames(replicate(3L, list1, simplify = FALSE), c('foo', 'bar', 'baz'))

Either way, don’t dynamically create variables. It’s never an appropriate solution for this kind of problem. Instead, it makes the code more complex and harder to understand. To keep code as simple as possible, variables should only be created explicitly in code (i.e. by assignments or as function parameters, etc.).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

Note that it's in general a very bad idea to change variable names dynamically. For all intents and purposes, you should consider variable names to be static, and tied to the code you write. (as commented by @Konrad Rudolph). See for example https://www.techrepublic.com/blog/it-security/never-use-dynamic-variable-names/

If you really want to use this, you can use assign(ff,list1). This creates a variable called Bar and contains the list

CIAndrews
  • 1,046
  • 10
  • 19
  • 3
    Please don’t glibly answer such misguided questions without first explaining in detail why this is a bad idea. We get tons of questions from beginners on here who’ve been misled into writing code like this, and consequently end up with code that they themselves no longer understand and need help with. – Konrad Rudolph Jan 05 '22 at 10:11
  • Fair point. Added explanation – CIAndrews Jan 05 '22 at 10:14