3

The question: Generate a response vector Y of length n = 100 according to the cubic model: =0+1+2^2+3^3+ where β0, β1, β2, and β3 are constants of your own choice.

I am trying to assign random integers to the 4 beta's. I wonder if I can assign them with different values in one line in R?

My approach is as follows:

beta_0, beta_1, beta_2, beta_3 = c(1, 2, 3, 4)

Thank you!

1 Answers1

4

One option is list2env by placing it in a named list

list2env(setNames(as.list(1:4), paste0("beta_", 0:3)), envir = .GlobalEnv)

But, it may be better to work on the list itself instead of creating multiple objects in the global environment

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Hello @akrun, when you mention that _"it may be better to work on the list itself"_, you mean because it is less object-load in the global environment, or because it is easier to assign the values in a compact way? If the last one, could you provide an example of it? Thank you a lot in advance. – Álvaro A. Gutiérrez-Vargas Nov 16 '20 at 11:36
  • 1
    @ÁlvaroA.GutiérrezVargas I meant it is easier to work on the `list` and later write it back as you can always use `lapply/sapply/vapply` etc to loop over the list and do the transformations – akrun Nov 16 '20 at 19:02