1

I'd like to create 3 empty lists and assign them to different variables.

blue <- list()
green <- list()
red <- list()

Is there a possibility of a simultaneous assignment?

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
s28
  • 173
  • 5
  • 2
    Do you really need them to be separate variables in your global environment? That's usually a red flag as far as best programming practives go. Normally it's much easier to work with similar data values inside a list itself. And if you are initializing with empty lists, that kind of implies you are going to be filling them up later and appending one item at a time to a list can be a slow operation in R. If a named list works, then `Map(function(...) list(), c("blue","green","red"))` would do the trick. You could use `list2env` is you really need to. – MrFlick Jun 05 '23 at 18:44
  • You are right, I'm using a for-loop to 'fill' the lists and the operation is way too slow... `for(i in 1:length(files){ blue[[i]] <- matrix.create(files[i]) red[[i]] <- vector.create(files[i]) green[[i]] <- vector.create(files[i]) }` would it work using `map`? – s28 Jun 05 '23 at 18:54
  • And yes, the operation is very slow. `length(files)` is 20000. – s28 Jun 05 '23 at 18:58
  • How do I know if I need them as separate variables in the global environment? – – s28 Jun 05 '23 at 19:07
  • 1
    I don't know what these `vector.create` and `matrix.create` functions do. What packages are you using? It's better to initialize lists to their final size first and then fill them rather than assigning into non-existing indices. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 05 '23 at 20:30

1 Answers1

2

Yes, it's possible.

red <- blue <- green <- list()

ls()
[1] "blue"  "green" "red"  

Note, that the lists at first have the same memory address,

sapply(list(red, blue, green), data.table::address)
# [1] "0x55ee16ce46a0" "0x55ee16ce46a0" "0x55ee16ce46a0"

but it will change as soon the objects are manipulated (i.e. a copy is created).

red[[1]] <- 'foo'; blue[[1]] <- 'foo'; green[[1]] <- 'foo'

sapply(list(red, blue, green), data.table::address)
# [1] "0x55ee16f1ea48" "0x55ee16f1e930" "0x55ee16f1e818"
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    Oh, thats nice! – s28 Jun 05 '23 at 18:55
  • will these reference the same list object or different objects? doing something like this in python will make all variables point to the same list. – qwr Jun 05 '23 at 19:09
  • I tried some simple loops and it seems that the variables point to different lists. – s28 Jun 05 '23 at 19:14
  • 1
    @qwr Yes, but this shouldn't matter, since addresses will change as soon you manipulate the objects. See edited answer. – jay.sf Jun 05 '23 at 19:17
  • If for some reason we're not happy with this issue, we can still use the `replicate` solution from [edit history](https://stackoverflow.com/revisions/76409194/1). – jay.sf Jun 05 '23 at 19:29
  • Ok, I wasn't sure about R's copy on modify mechanic (I think it's covered in Advanced R by Hadley) – qwr Jun 06 '23 at 15:52