-1

This is what I have now:

models = list()
models$A = 5700
models$Ą = 8600
models$B = 12400
models$C = 10000
models$Ć = 14900
models$D = 12200
models$E = 7700
models$Ę = 9800
models$F = 3600
models$G = 13200
models$H = 8400
models$I = 5500
models$J = 14900
models$K = 8200
models$L = 9900
models$Ł = 7100
models$M = 7100
models$N = 9300
models$Ń = 12100
models$O = 10200
models$Ó = 5600
models$P = 14200
models$R = 15000
models$S = 14800
models$Ś = 8800
models$T = 10400
models$U = 8500
models$W = 13300
models$Y = 7000
models$Z = 8000
models$Ź = 7800
models$Ż = 12400

I refuse to believe that the above is the simplest way to initialize such a list.

Is there any list literal syntax in R?

  • 2
    What's your input and expected output? Your question is unclear, please read and edit your question according to [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so that other users can help you. – pogibas Jun 14 '19 at 11:51
  • 1
    @PoGibas Why does it matter? I only wish to have such an association as I showed above. I want `models[['A']]` to evaluate to `5700`. If input/output really matters: I have files in a directory whose names are of the form `A_model_5700`, `Z_model_8000`, etc - so I want to hardcode the association that will allow the program to subsequently open the correct file. –  Jun 14 '19 at 11:55
  • @PoGibas Why is my question unclear? I don't know how could I make a runnable example - except for providing some artificial code like `print(models[['Z']]) # should print 8000`. I want a shorter code that is equivalent to what I wrote above, that's all. Does defining a list not count as a reproducible effect? –  Jun 14 '19 at 11:58
  • Thanks! Now I got it – pogibas Jun 14 '19 at 11:59

1 Answers1

0

You can look at ?as.list. If you have a named list it would directly assign the names

x <- c(A = 5700, B = 8600, C = 12400, D = 10000)
as.list(x)
#$A
#[1] 5700

#$B
#[1] 8600

#$C
#[1] 12400

#$D
#[1] 10000

Or if you have numbers and names differently you can use setNames to name them

x <- c(5700, 8600, 12400, 10000)
setNames(as.list(x), LETTERS[1:4])
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213