Re: [R] .Last.value or % or ANS or ... (was unix)

About this list Date view Thread view Subject view Author view Other groups

Subject: Re: [R] .Last.value or % or ANS or ... (was unix)
From: Mark Myatt (mark@myatt.demon.co.uk)
Date: Wed 08 Nov 2000 - 18:50:02 EST


Message-ID: <ktZxDAA6ORC6EwWS@myatt.demon.co.uk>

Bill Simpson <wsi@gcal.ac.uk> writes:

>On Tue, 7 Nov 2000, Mark Myatt wrote:
>
>> Yves Gauvreau <cyg@sympatico.ca> writes:
>> >... and Matlab uses "ans" and Mathematica effectively uses %, %%
>> >
>> >Would be nice to have something simpler than .Last.value
>>
>> Perhaps I am stating the obvious or being extremely stupid but you can
>> create a function:
>>
>> lv <- function()
>> {
>> .Last.value
>> }
>>
>> And then use lv() whenever you need .Last.value.
>
>How do you say "give me the output I saw three steps ago?" In Mathematica
>you would use %%% or Out[-3].

Why not implement a LIFO stack? Here is a first attempt:

        #
        # Flush the stack
        # (call this to create the stack)
        #
        flush <- function()
         {
         stack <<- list()
         }
 
        #
        # Push a value onto the stack
        #
        push <- function()
         {
         stack <<- c(stack, .Last.value)
         }

        #
        # Pull a value from the stack
        # (default is last pushed value)
        #
        pull <- function(x = 0)
         {
         stack[[length(stack) - x]]
         }

        #
        # Pull a value from the stack and remove it
        # (default is last pushed value)
        #
        pop <- function(x = 0)
         {
         pop <- stack[[length(stack) - x]]
         stack <<- stack[-length(stack) + x]
         pop
         }

You still need to push() the values you want to keep onto the stack.
Another limitations of the above code is that objects are stored as
'atoms':

> flush()
> a <- c(1,2,3,4,5)
> mean(a)
        [1] 3
> push()
> stack
        [[1]]
        [1] 3

> a
        [1] 1 2 3 4 5
> push()
> stack
        [[1]]
        [1] 3

        [[2]]
        [1] 1

        [[3]]
        [1] 2

        [[4]]
        [1] 3

        [[5]]
        [1] 4

        [[6]]
        [1] 5

> pull(1)
        [1] 4
> pop(1)
        [1] 4
> stack
        [[1]]
        [1] 3

        [[2]]
        [1] 1

        [[3]]
        [1] 2

        [[4]]
        [1] 3

        [[5]]
        [1] 5

Which makes it pretty useless for vector and matrix results but there
should be a way round that.

Mark

--
Mark Myatt

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b25 : Thu 01 Feb 2001 - 16:14:31 EST