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--
  • 20,766
  • 7
  • 52
  • 87
fny
  • 27,890
  • 15
  • 90
  • 116

0 Answers0