In my opinion, the best usages of Python and R should be for different type
of tasks respectively. For example, Python is good for automating
miscellaneous tasks, while R is good for list data processing and
statistical modelling. Therefore when you become more familiar with Python
and R, you shall not use the two for exactly the same thing.
-----Original Message-----
From: sun shine
Sent: Sunday, December 29, 2013 5:32 PM
To: r-help@r-project.org
Subject: Re: [R] Translating a basic Python script into R
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
______________________________________________
R-help@r-project.org mailing list
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.
______________________________________________
R-help@r-project.org mailing list
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.