【发布时间】:2016-04-30 23:49:36
【问题描述】:
每当我使用应用函数时,在匿名函数中使用虚拟变量都会导致在内部使用该虚拟变量的名称。在处理结果列表时,如何在内部使用原始变量名以避免复杂化?
以下是描述我的意思的示例:
set.seed(314)
df <- data.frame(response = rnorm(500),
Col1 = rnorm(500),
Col2 = rnorm(500),
Col3 = rnorm(500),
Col4 = rnorm(500))
> apply(df[, 2:5], 2, function(x) lm(response ~ x, data = df))
$Col1
Call:
lm(formula = response ~ x, data = df)
Coefficients:
(Intercept) x
0.074452 0.007713
$Col2
Call:
lm(formula = response ~ x, data = df)
Coefficients:
(Intercept) x
0.06889 0.07663
$Col3
Call:
lm(formula = response ~ x, data = df)
Coefficients:
(Intercept) x
0.07401 0.03512
$Col4
Call:
lm(formula = response ~ x, data = df)
Coefficients:
(Intercept) x
0.073668 -0.001059
我希望上面的每个线性回归在每个回归中都使用名称 Col1、Col2 等,而不是 x。此外,当我使用 apply 函数时,我正在寻找一种在任何情况下(不仅仅是线性回归)使用原始名称的通用方法。
【问题讨论】:
标签: r