[R] Unexplained behaviour of .Last.value
Jake Elmstedt
j@ke@e|m@tedt @end|ng |rom gm@||@com
Sat Jun 19 09:56:48 CEST 2021
Behaviour is confirmed on 4.1.0 on Ubuntu 20.04 and Windows 10.
What's happening is
(sin(.Last.value) + sin(2 * .Last.value))
is evaluating the first sin(.Last.value) which resolves to 0.7680514
then using that in the second expression, sin(2*.Last.value) := sin(2
* 0.7680514) which resolves to 0.9993982. Add them together to get the
1.76745 value you're showing.
Interestingly, if you're using R Studio, if you go to Tools > Global
Options > General > Advanced > Other and check "Show .Last.Value in
environment listing," then
acos((-sqrt(17) - 1) / 8)
# [1] 2.2658
(sin(.Last.value)+sin(2*.Last.value))
# [1] -0.2156507
Sadly, .Last.value isn't very well documented. The only thing I see in
the manuals is in R-ints,
https://cran.r-project.org/doc/manuals/r-release/R-ints.html#Base-environment
> Similarly, the evaluator creates a symbol .Last.value which appears as a variable in the base environment.
Playing around though, it seems if the last value is the result of a
symbol lookup or assignment, .Last.value seems to do something like
"re-evaluate" the symbol, resetting the .Last.value value to the
stored value each time, whereas if the last expression evaluated
returns a pure value, calling .Last.value in an expression can update
the .Last.value value.
1
#> [1] 1
# This is evaluated as 1 + (1) + (2) + (4) + (8) as .Last.Value
changes each time.
.Last.value + .Last.value + .Last.value + .Last.value + .Last.value
#> [1] 16
x <- 1
# This is evaluated as 1 + 1 + 1 + 1 + 1
.Last.value + .Last.value + .Last.value + .Last.value + .Last.value
#> [1] 5
x <- 1
x
# This is still evaluated as 1 + 1 + 1 + 1 + 1
.Last.value + .Last.value + .Last.value + .Last.value + .Last.value
#> [1] 5
1
# This is also evaluated as 1 + 1 + 1 + 1 + 1
(x <- .Last.value) + .Last.value + .Last.value + .Last.value + .Last.value
Created on 2021-06-19 by the [reprex
package](https://reprex.tidyverse.org) (v2.0.0.9000)
The lesson here is .Last.value should (almost) never be used
programmatically, and in an interactive context the only approved use
of it is to store a result before losing it.
On Fri, Jun 18, 2021 at 11:03 PM Bickis, Mikelis <bickis using math.usask.ca> wrote:
>
> Hello:
>
> Here is a bit of R-code:
>
> > mvr
> [1] 2.2658
> > (sin(.Last.value)+sin(2*.Last.value))
> [1] -0.2156507
> > acos((-sqrt(17)-1)/8)
> [1] 2.2658
> > (sin(.Last.value)+sin(2*.Last.value))
> [1] 1.76745
> > (sin(mvr)+sin(2*mvr))
> [1] -0.2156507
> >
>
> Note that my variable mvr is the value of acos((-sqrt(17)-1)/8). However, if I use .Last.value after invoking the acos function in a further calculation, I get a nonsensical result. What is going on?
>
> I am running R version 4.0.0 (2020-04-24) on an iMac, operating system MacOS 11.4 “Big Sur”.
>
> Mik Bickis
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list