1

What's the proper way to extract a variable by name from a list dynamically without altering the underlying data type? I originally thought I could do it with thelist[varname], but the bracket operation always mangles the value into a list. Below is an example:

df <- data.frame(x,y,z)
names(df) <- c('x','y','z')
lms = list(
  lm1 = lm(y ~ x, data = df),
  lm2 = lm(z ~ x, data = df)
)

class(lms$lm1) # => lm
class(lms['lm1']) # => list
M--
  • 25,431
  • 8
  • 61
  • 93
fny
  • 31,255
  • 16
  • 96
  • 127

0 Answers0