.Last.value

The .Last.value command gets the value returned by the most recent R command. .Last.value is most useful when coding on the fly.

Example. Data for a generalized linear model (GLM) is generated and a GLM model is fit to these data. In the first use the .Last.value command, I obtained the random numbers used to generate x and used identical to verify they were the same. This demonstration is important: .Last.value is actually the last value, not just the last command run again. Later in the code, I used .Last.value to grab the glm output without retyping or calling the previous line.

> set.seed(5) > N <- 50 > x <- rnorm(N) > temp <- .Last.value > identical(x, temp) [1] TRUE > > lp <- -2 + 3*x > p <- exp(lp) / (1 + exp(lp)) > y <- ifelse(runif(N) < p, 1, 0) > > X <- seq(-5, 5, 0.01) > LP <- -2 + 3*X > P <- exp(LP) / (1 + exp(LP)) > > png("LastValue.png") > plot(X, P, xlim=range(x), ylim=0:1, + type="l", ylab="Probability") > points(x, y, col=2) > dev.off() null device 1 > > glm(y ~ x, binomial) Call: glm(formula = y ~ x, family = binomial) Coefficients: (Intercept) x -2.625 3.418 Degrees of Freedom: 49 Total (i.e. Null); 48 Residual Null Deviance: 61.09 Residual Deviance: 21.92 AIC: 25.92 > model <- .Last.value > model Call: glm(formula = y ~ x, family = binomial) Coefficients: (Intercept) x -2.625 3.418 Degrees of Freedom: 49 Total (i.e. Null); 48 Residual Null Deviance: 61.09 Residual Deviance: 21.92 AIC: 25.92 > > summary(model) Call: glm(formula = y ~ x, family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -1.8604 -0.2764 -0.0934 0.2606 2.7035 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -2.6251 0.9312 -2.819 0.00482 ** x 3.4185 0.9865 3.465 0.00053 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 61.086 on 49 degrees of freedom Residual deviance: 21.916 on 48 degrees of freedom AIC: 25.916 Number of Fisher Scoring iterations: 7

Tip. The .Last.value command is really useful when needing to obtain the value from a computationally heavy command that was accidentally not saved.