We help IT Professionals succeed at work.

Code translation from R to Python

Hi all,

I need some help with some code translation.  Specifically, I have a program written in R and I have been tasked with converting it Python.  However, I have more experience in R than in Python, but I am slowly learning.  I have searched for some syntax translation for R and Python online and have found some of the equivalents for NumPy (numerical python), but I am missing some.   Here is a list of R syntax that I need Python syntax equivalents for:

gsub
as.numeric
tapply
sapply
sweep
names
dimnames

Many thanks,
Kindra
Comment
Watch Question

Well, I do not know MatLab. The gsub() seems to be "global substitution" -- or it is str.replace() http://docs.python.org/library/stdtypes.html#str.replace, or it is re.sub() http://docs.python.org/library/re.html#re.sub if it allows regular expressions (better to use rex = re.compile() and then rex.sub() if used intensively).

Author

Commented:
Thanks pepr, well it turns out I was able to find a resource online here: http://www.docstoc.com/docs/20492925/MATLAB-commands-in-numerical-Python.  I was fully anticipating that I would have to do some detective work to translate some of the syntax, but I was hoping someone here might have done it already!  I am going to give this question another day or so and if no one else comes through, you'll get full points.  =)
Thanks, but as it was only my wild guess, I suggest it would be more fair to keep the question open for a while and ask for more details in the comments, or...

In other words, I did not answer your question. If you found the solution in the doc that you pointed out, then you should probably accept your own solution, and you will get your points back. ;)

Author

Commented:
Thanks for being so genuine pepr...I will keep it open for a few more days and then close it.
Commented:
Converting R to MATLAB or Python/Numpy is not a strait-forward task. Some language concepts are different and some functions has to be converted depending on context. If you really have to do it, here some resources on basic equivalents between those languages:

NumPy for R (and S-Plus) users:
http://mathesaurus.sourceforge.net/r-numpy.html

NumPy for Matlab Users:
http://www.scipy.org/NumPy_for_Matlab_Users

Great MATLAB<-> R Reference (PDF):
http://www.math.umaine.edu/~hiebeler/comp/matlabR.html

Anyway, if you will have a problem during translation, just make a question.

Author

Commented:
Thanks to pepr and yuk99 for providing some points of reference to check out.  I am familiar with NumPy and actually, another good tool is RPy for simple conversion.  But as yuk99 stated, there is really no straight-forward conversion methodology out there to convert R to python. The best solution is to fully understand the functionality of the syntax for R and find its equivalent in Python, or vica versa.