[R] Translating a basic Python script into R

sun shine phaedrusv at gmail.com
Sun Dec 29 10:32:37 CET 2013


Hi Ista

On 28/12/13 23:06, Ista Zahn wrote:
> Hi,
>
> <snip>
> I don't see any nested conditions in the python code... A direct
> translation in R looks almost the same, except that you need to group
> using parentheses and brackets instead of whitespace, and there is no
> += in R (at least not that I'm aware of). Making those changes gives
>
>    stock = 50
>    time = 1
>    inflow_a = 0
>    inflow_b = 5
>    outflow = 5
>    x = stock
>    y = time
>
>    print ("Model of inflow and outflow rates of water")
>    print ("version 3")
>
>    print (stock)
>    while (time <= 9) {
>
>        stock = (stock - outflow) + inflow_a
>        time = time + 1
>        y = c(y, time)
>        x = c(x, stock)
>        print (stock)
>        if (stock == 30) {
>            print ("Faucet turned on")
>        }
>    }
>
>    while (time >= 6 & time <= 9) {
>        stock = (stock - outflow) + inflow_b
>        time = time + 1
>        y = c(y, time)
>        x = c(x, stock)
>        print (stock)
>    }
>        sprintf("Volume in tub stabilises at %d gallons over %d
> minutes", stock, time)
>        print (x)
>        print (y)
>
>
>> I'm sure that there must be some very elegant way to do this, but I cannot
>> find out how to do so in any of the books I have, nor do my web searches
>> throw back anything useful (I suspect that I'm not phrasing the question
>> properly).
> In both python and R you can of course use if/else instead of the two
> separate while loops. An R version is
>
>    stock = 50
>    time = 1
>    inflow_a = 0
>    inflow_b = 5
>    outflow = 5
>    x = stock
>    y = time
>
>    print ("Model of inflow and outflow rates of water")
>    print ("version 3\n")
>
>
>    print (stock)
>    while (time <= 9) {
>        if(time <= 5) {
>            stock = (stock - outflow) + inflow_a
>        } else {
>            stock = (stock - outflow) + inflow_b
>        }
>        time = time + 1
>        y = c(y, time)
>        x = c(x, stock)
>        print (stock)
>        if (stock == 30) {
>            print ("Faucet turned on")
>        }
>    }
>
>    sprintf("Volume in tub stabilises at %d gallons over %d minutes", stock, time)
>    print (x)
>    print (y)
>    plot(y, x)
>
>
>> Can someone please offer a few suggestions about ways that I could translate
>> the Python script into R so that I can then run a plot as well?
> You can plot in python, e.g.,
>
>    from matplotlib.pyplot import *
>    plot(y, x)
>    show()
>
> Best,
> Ista
>
This was *very* helpful: I leaned about both R and Python and am pleased 
to see that the structure between the two - for this script at least - 
are so similar.

Thank you for taking the time to explain and demonstrate rather than to 
just tell me to RTFM. Your reply has given me a lot of ideas to play 
around with in experimenting, so I can envisage an enjoyable afternoon 
testing some of this on the other models Meadows described.

Many thanks for your clear explanations.

Best wishes

Sun



More information about the R-help mailing list