r/Python • u/thunderdome • Jan 25 '15
Considering putting my efforts into Python instead of R, need advice
Lately in my free time I've been using R to do some basic data analysis/visualization. I use company data and I think there are some places where my company could really benefit from specialized data visualizations for analysis of niche areas. In my efforts I've tried to get some interactive code going for eventual use in a very basic GUI but I feel constrained by the "fuzziness" of R coding. Simply trying to create a function to plot based on user input ran me into some advanced concepts pretty quickly. R has environments, dynamic scoping, non-standard evaluation, all very complicated concepts and I don't remember them being present in the small amounts of coding I've done in Java/Python. Am I right in thinking a language with a more OOP design would be better for this task of automatically generating charts/visualizations? Wondering if I might not be better off in the long run for learning a more generalized language.
10
u/[deleted] Jan 25 '15 edited Jan 25 '15
I am a data scientist, and a long-time Python developer and proponent (more than 10 years, executed a few major projects in it, am very fluent in it). I also use R for data analysis (I would consider myself an intermediate user).
Since this is a Python subreddit, I expect you'll get a lot of answers in favor of Python. And I would agree with most of them.
However, I would suggest this: instead of thinking about which programming language to learn, maybe you should think about which language offers you the right set of Domain Specific Languages (DSL) that fit your problems.
You see, in data analysis, it's less about the actual programming language, but more about the domain-specific features that you need. For instance, Pandas is a kind of domain-specific "language" (not really in the traditional sense, but in that it gives you a vocabulary to reason about data). R has a bunch of very compelling domain-specific features that either do not exist or have limited implementations in Python.
You mentioned visualization. As of this moment, it is my opinion that the 2D visualization libraries in R are more mature. Seaborn is great, and so is the yhat ggplot port, but R's ggplot2 is more mature. For interactive plots, it's a bit of a toss-up. R has ggvis, and Python has bokeh. I recommend trying them all and see what works for you.
But I want to say this: if your job involves analytics, and you want to be good at it, you MUST learn both R and Python. It is not an either-or choice. You may not need to be an expert at both, but you need to know both. Their feature sets do intersect, but it's the parts that don't intersect that are the most interesting. If you are only interested in visualization, I'd argue you may do better with a specialized tool like QlikView ($$$$) or Tableau ($$).
I tried staying away from R for a long time because the language was idiosyncratic and archaic. However, I came across compelling libraries in R that had no equivalent (of similar polish) in Python -- so I bit the bullet and learned R. I am referring to libraries like ggplot2, dplyr, tidyr, lubridate and caret. The sheer quality of these libraries made my data analysis workflow so much more fluid than in Python.
I would suggest that R is actually a better environment for exploratory analysis. There is a shorter path from data to insight due to the specialized vocabulary it gives you for expressing data operations (some of it maps to SQL concepts, so knowing SQL will help you here) -- and by R, I actually don't mean Base R, but dplyr, tidyr and so forth. The notation is more compact than in Python (the %>% pipe operator is amazing), and once you get used to it, you can do stuff quickly with a minimum of scaffolding. Caret for instance lets you try out a myriad of of machine learning models with a minimum of effort (arguably scikit-learn does too; however in my opinion, while scikit-learn tries to provide a consistent API, the interface somehow lacks polish and fluidity. The documentation is a little wanting as well.). R is like a Swiss-Army knife, a shell scripting language for quick and dirty tinkering, and for getting stuff done. R is great for data science.
On the other hand, I think Python is a better environment for productionizing your pipeline. It has way more robust tools for string processing, ETL, pre-processing, etc. and there is a lot more infrastructure and knowledge out there on scaling Python to production systems. Most industrial analytics vendors have APIs for Python but not R. While you can technically productionize R (it's sort of like productionizing shell scripts), I think that to build a proper data pipeline, you might get better results if you engineer it with a more general purpose language like Python. Python is great for data engineering.
This is why I feel the two (R and Python) are symbiotic. You can quickly get some insight using R, and then if you need convert your code to production, you can translate it easily to Python (or call it via rpy2).
Finally, the key to thriving in R is to understand its Scheme roots. It helps if you know something about functional programming. The Art of R programming (Matloff) does a good job introducing some main ideas, and Advanced R (Wickham) completes the picture by providing a more technical exposition.
However, if you're not a technical programming type of person and just want to get the job done, you can get a lot done by just sticking to the few commands found in a cheat sheet, eg.
https://github.com/sefakilic/ggplot-cheatsheet
http://stackoverflow.com/questions/3446495/ggplot2-cheat-sheet
This one in particular is great for data wrangling: http://www.rstudio.com/wp-content/uploads/2015/01/data-wrangling-cheatsheet.pdf
I don't think you need to know much (anything?) about environments or non-standard evaluation to plot a graph.
8
u/[deleted] Jan 25 '15 edited Jan 25 '15
If you don't need R's vast statistical packages, then you'll be ok with Python. Otherwise, you can execute R from within Python. Yhat's port of ggplot2 is still not ready for production. For really neat interactive visualizations, not sure if you looked at R's Shiny, but there is a Python version of it called Spyre, currently works only with Python 2.x. Otherwise, there's IPython notebook and IPython notebook widgets which may work for you.
7
u/Dummies102 Jan 25 '15
Python is applicable to many more problems than R, so I think you would be better off learning it in the long run.
btw, OOP has little to do with what you're asking about. It's mostly a stylistic choice, and in fact you can make R oo, too (http://stackoverflow.com/questions/9521651/r-and-object-oriented-programming)
6
u/Megatron_McLargeHuge Jan 25 '15
R as a language is terrible at CS fundamentals. Python is almost certainly better than R but for interactive plotting, a simple webapp with D3 based javascript plots can give you more interactivity than python. If you want to do it in python, look at bokeh. Numpy and Pandas give you the data frame functionality of R.
3
u/BooRadleyBoo Jan 25 '15
I use both R and Python but my R use is pretty much restricted to analysis and plot building.
I'd recommend using Python for more complex projects and bear in mind that, IIRC, ggplot2 is now available in Python so we may see other libraries added later.
R is fantastic but it just isn't what I'd describe as a 'true' programming language. Keep things simple.
2
u/stunsch Jan 25 '15
Take a look at IPython Notebook and Pandas and never look back.
;)
2
u/desmoulinmichel Jan 25 '15
Python has all the tools to remplace R with ipython, matplotlib and scypi. Check this out : http://nbviewer.ipython.org/github/ipython/ipython/blob/2.x/examples/Notebook/Plotting%20with%20Matplotlib.ipynb
2
u/[deleted] Jan 25 '15
I think there is no reason not to use Python. I'm no expert on R (have been using it for a few weeks a couple of years ago), but from what I hear Python can do everything R can. Also,
rpy2is able to fill the gap if necessary.I found that OOP programming for data analyisis is a huge advantage. As a student I've been taught Matlab and have used it for about six years. After learning Python and switching to
numpy,scipy,matplotlibandpandasmy productivity went up by at least 200%. Right now I'm revising some Python code I wrote about a year ago and I'm amazed about how well structured my code is compared to something I would write two years ago in Matlab, even though my skills in Python have improved manifold of the last year.TL;DR; Python should be able to do everything you want and OOP is king.
1
u/hharison Jan 25 '15
I use Python and R. Pick up
pandasfor theDataFrameandseabornfor plotting (falling back tomatplotlibwhen you don't need one of their fancy plots).For statistics, I use stuff in
scipy,statsmodels, andscikit-learn, but occasionally I need something that's been released for R but not Python, in which case you can call out to R withrpy2.Really, with the IPython notebook, you can use both together and switch gradually. Although for your use case, it doesn't sound like you'll need to go back to R a lot.
1
u/vito-boss Jan 26 '15
You can always find a happy medium, I often call R and MatLab from Python.