As data science becomes more and more applicable across every industry sector, you might wonder which programming language is best for implementing your models and analysis. Since R and Python remain the most popular languages for data science, according to IEEE Spectrum's latest rankings, it seems reasonable to debate which one is better. Although it's suggested to use the language you're most comfortable with and one that suits the needs of your organization, for the purpose of this article, we'll evaluate the two languages. We will compare R and Python in four key categories: Data Visualization, Modelling Libraries, Ease of Learning and Community Support.
A significant part of data science is communication. Most of the time, you as a data scientist need to show your results to colleagues with little or no background in mathematics or statistics. So being able to illustrate your results in an impactful and intelligible manner is very important. Any programming language or software package for data science should have good data visualization tools.
Good data visualization involves clarity. No matter how complicated your model is, there will be a simple and unambiguous way of illustrating your results such that even a layperson would understand.
Python is renowned for its extensive number of libraries. There are plenty of libraries that can be used for plotting and visualizations. The most popular Python visualization libraries are matplotlib and seaborn. The library matplotlib is adapted from MATLAB and has similar features and styles. It’s a very powerful visualization tool with all kinds of functionality built-in. It can be used to easily make simple plots, especially as it works well with other Python data science libraries, pandas and numpy.
Although, matplotlib can make a whole host of graphs and plots, what it lacks is simplicity. The most troublesome aspect is adjusting the size of the plot, and if you have a lot of variables it can get hectic trying to neatly fit them all. Another big problem is creating subplots; again adjusting them all in one figure can get complicated.
Now, seaborn builds on top of matplotlib, including more aesthetic graphs and plots. This library is surely an improvement on matplotlib’s archaic style, but it still has the same fundamental problem: creating figures can be very complicated. However, recent developments have tried to make things simpler.
There are many libraries that can be used for data visualization in R, ggplot2 is the clear winner in terms of usage and popularity. The library uses a grammar of graphics philosophy, with layers used to draw objects on plots. Layers are often interconnected to each other and can share many common features. These layers allow one to create very sophisticated plots with very few lines of code. The library allows plotting of summary functions. Thus, ggplot2 is more elegant than matplotlib and seaborn and thus I feel that in this department R clearly has an edge.
It is, however, worth noting that Python includes a ggplot library based on the similar functionality as the original ggplot2 in R. It is for this reason that R and Python both are on par with each other in this key category.
Data science requires the use of many algorithms. These sophisticated mathematical methods require robust computation. It is rarely or maybe never the case that you as a data scientist need to code the whole algorithm on your own. Since, that is incredibly inefficient and sometimes very hard to do so, data scientists need languages with built-in modelling support. One of the biggest reasons why Python and R get so much traction in the data science space is because of the models you can easily build with them.
As mentioned earlier Python has a very large number of libraries. So naturally, it comes as no surprise that Python has an ample amount of machine learning libraries. There is scikit-learn, XGboost, TensorFlow, Keras and PyTorch just to name a few. Python also has pandas, which allows tabular forms of data. The pandas library makes it very easy to manipulate CSVs or Excel-based data.
In addition, Python has great scientific packages like numpy. Using numpy, you can do complicated mathematical calculations like matrix operations in an instant. All of these packages combined, make Python a powerhouse suited for hardcore modelling.
R was developed by statisticians and scientists to perform statistical analysis way before that was such a hot topic. As one would expect from a programming language made by scientist, one can build a plethora of models using R. Just like Python, R too has plenty of libraries approximately 10000 of them. The mice package, rpart, party and caret are the most widely used. These packages will have your back starting from the pre-modelling phase to the post-model/optimization phase.
Since you can use these libraries to solve almost any sort of problem; for this discussion let’s just look at what you can’t model. Python is lacking in statistical non-linear regression (beyond simple curve fitting) and mixed effects models. Some would argue that these are not major barriers or can simply be circumvented. True! But when the competition is stiff you have to be nitpicky in order to decide which is better. R, on the other hand, lacks speed that Python provides, which can be useful when you have large amounts of data (big data).
Ease of Learning
Its no secret that currently data scientist is one of the most in demand jobs, if not the one most in demand. As a consequence, many people are looking to get into the data science bandwagon, many of them have little or no programming experience. Learning a new language can be challenging, especially if it is your first. For this reason it appropriate to include ease of learning as a metric when comparing the two languages.
Designed in 1989 with a philosophy that emphasizes code readability and a vision to make programming easy or simple. The designers of Python clearly succeeded as the language is fairly easy to learn. Although Python takes inspiration for its syntax from C, unlike C it is uncomplicated. I recommend it as my choice of programming language for beginners, since anyone can pick it up in relatively less time.
In this category Python is the clear winner. However, it must be noted that programming languages in general are not hard to learn. If a beginner wanted to learn R, it won’t be as easy in my opinion as learning Python but it won’t be an impossible task either.
Every so often as a data scientist you are required to solve problems that you haven’t encountered before. Sometimes you may have difficulty finding the relevant library or package that could help you solve your problem. To find a solution, it is not uncommon for people to search in the language’s official documentation or online community forums. Having a good community support can help programmers in general to work more efficiently.
Both of these languages have active Stackoverflow members and also an active mailing list available (where one can easily ask for solutions from experts). R has online R-documentation where you can find information about certain functions and function inputs. Whereas most Python libraries like pandas and scikit-learn have their own official online documentation that explains each library.
Both languages have significant amount of user base, hence, they both have a significantly active support community. It isn’t difficult to see that both seem to be equal in this regard.
So, which one is better: R or Python? Well, having analyzed all the different categories one comes to the conclusion that both are great programming languages. What you prefer amongst the two is based on what type of situation they are in. If you’re new to programming go for Python since it is relatively easier to learn. If you want to easily create visuals, then R is the language for you. But in the end, starting with either language and consistently practicing in that language is key to successfully implementing data science programmatically.
This is a companion discussion topic for the original entry at https://blog.datasciencedojo.com/r-vs-python-which-is-better-for-data-science/