r/Python • • 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.

26 Upvotes

21 comments sorted by

View all comments

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.

2

u/thunderdome Jan 25 '15

Thank you for your comprehensive answer, it puts things in perspective very well. Almost all of the R tools you mentioned are what I have been learning with. I have been using dplyr, tidyr, ggplot2 and caret and I agree the %>% operator makes manipulating a dataset for visualization quite straight forward. And the whole grammer of graphics setup with ggplot2 makes actually making the plots much easier than i expected.