[R] Fwd: the same function returning different values when called differently..

Martin Maechler maechler at stat.math.ethz.ch
Wed Mar 14 17:38:02 CET 2018


>>>>> Eric Berger <ericjberger at gmail.com>
>>>>>     on Wed, 14 Mar 2018 18:27:38 +0200 writes:

    > Hi Akshay, (Please include r-help when replying)

    > You have learned that PFC.NS and snl[[159]] are not
    > identical. Now you have to figure out why they
    > differ. This could also point to a bug or a logic error in
    > your program.  Figuring out how two objects differ can be
    > a bit tricky, but with experience it becomes easier. (Some
    > others may even have some suggestions for good ways to do
    > it.)  Basically you would work your way down. At the top
    > level is the class of each, which you already tested and
    > they are identical.

    > Now try:

    >> str(PFC.NS)

    > and compare it to

    >> str(snl[[159]]

Note further the

     all.equal(obj1, obj2)

maybe a good start to find differences between R objects 'obj1' and 'obj2',
as all.equal() is generic and has a list method
(which works recursively) the "output may be huge, but then if there
is huge number of differences you would have found yourself
anyway, and will not need all.equal()

Martin Maechler
ETH Zurich and R Core


    > Look closely at the two outputs to try to detect
    > differences. If they are the same then you will have to
    > examine the sub-objects described by str().  You didn't
    > mention what type of objects they are. Suppose they are
    > both "numeric" vectors. Then you can check whether their
    > lengths are equal.  And you can compare their values,
    > etc. etc. No short cut that I can think of without more
    > information.

    > It definitely takes work to find discrepancies. Think of
    > it as a challenge and perhaps write functions that you can
    > use to automate this kind of comparison in the future.
    > (Again, other people on this list might be able to point
    > to tools that help with this for objects of specific
    > type.)

    > Good luck, Eric




    > dear Eric,

    >                    Bulls eye...! identical(PFC.NS,
    > snl[[159]])) is resulting false..but class(PFC.NS) and
    > class(snl[[159]]) are same...

    > I want snl[[159]] to be equal to PFC.NS...how do I effect
    > it? create a list with some other element list... for
    > example snl[[200]] == PFC.NS?


    > very many thanks for your time and effort...


    > yours sincerely,

    > AKSHAY M KULKARNI




    > ------------------------------
    > *From:* Eric Berger <ericjberger at gmail.com> *Sent:*
    > Wednesday, March 14, 2018 5:22 PM *To:* akshay kulkarni
    > *Cc:* R help Mailing list *Subject:* Re: [R] the same
    > function returning different values when called
    > differently..

    > Hi Akshay, Presumably PFC.NS and snl[[159]] are not
    > exactly the same.  You can start by trying to determine if
    > (and then how) they differ.  e.g.
    >> identical(PFC.NS, snl[[159]])
    > presumably this will result in FALSE

    > then compare
    >> class(PFC.NS) class(snl[[159]])

    > etc

    > HTH, Eric


    > On Wed, Mar 14, 2018 at 1:42 PM, akshay kulkarni
    > <akshay_e4 at hotmail.com> wrote:

    > dear members,

    >  I have a function ygrpc which acts on the daily price
    > increments of a stock. It returns the following values:


    >  ygrpc(PFC.NS,"h") [1] 2.149997 1.875000 0.750000 0.349991
    > 2.100006 0.199997 4.000000 2.574996 0.500000 0.349999
    > 1.500000 0.700001 [13] 0.500000 1.300003 0.449997 2.800003
    > 2.724998 66.150002 0.550003 0.050003 1.224991 4.899994
    > 1.375000 1.574997 [25] 1.649994 0.449997 0.975006 2.475006
    > 0.125000 2.625000 3.649994 0.399994 1.300003 2.074997
    > 1.025001 0.125000 [37] 3.849999 0.025002 0.824997 1.750000
    > 1.699997 1.750000 1.699997 0.275002 2.300003 0.349998
    > 0.750000 0.224998 [49] 0.125000 1.475006 1.599998 0.125000
    > 0.500000 0.700005 1.099998 0.225006 1.274997 1.300003
    > 3.000000 0.300003 [61] 0.724999 3.699997 2.424995 8.425003
    > 1.099991 2.025009 0.850006 4.000000 1.724991 0.949997
    > 1.825012 2.799988 [73] 0.425003 1.799995 5.700005 2.125000
    > 0.125000 4.000000 2.350006 1.524994 1.299995 0.300003
    > 0.949997 0.449997 [85] 1.899994 1.700005 1.150009 0.849998
    > 2.449997 5.300003 0.19999

    > I also have a list of stocks called "snl" with snl[[159]]
    > pointing to PFC.NS (above): tail(snl[[159]]) PFC.NS.Open
    > PFC.NS.High PFC.NS.Low PFC.NS.Close PFC.NS.Volume
    > PFC.NS.Adjusted 2018-03-07 98.40 98.45 95.1 95.30 7854861
    > 95.30 2018-03-08 94.90 94.95 89.3 91.90 12408061 91.90
    > 2018-03-09 92.00 94.50 91.9 93.10 7680222 93.10 2018-03-12
    > 93.40 93.85 86.1 88.25 12617833 88.25 2018-03-13 89.20
    > 91.85 86.2 89.85 12792630 89.85 2018-03-14 88.65 89.30
    > 86.1 86.70 16406495 86.70

    > But ygrpc(snl[[159]],"h") returns : ygrpc(snl[[159]],"h")
    > [1] 1

    > Can you please shed some light on what is happening?

    > Very many thanks for your time and effort....

    > Yours sincerely AKSHAY M KULKARNI







    >         [[alternative HTML version deleted]]

    > ______________________________________________
    > R-help at r-project.org mailing list -- To UNSUBSCRIBE and
    > more, see https://stat.ethz.ch/mailman/listinfo/r-help
    > R-help -- Main R Mailing List: Primary help - Homepage -
    > SfS <https://stat.ethz.ch/mailman/listinfo/r-help>
    > stat.ethz.ch The main R mailing list, for announcements
    > about the development of R and the availability of new
    > code, questions and answers about problems and solutions
    > using R ...


    > PLEASE do read the posting guide
    > http://www.R-project.org/posting-guide.html and provide
    > commented, minimal, self-contained, reproducible code.

    > 	[[alternative HTML version deleted]]

    > ______________________________________________
    > R-help at 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