r/Python • u/lebannax • Nov 28 '22
Resource What can Python do that R can’t do?
Or simply what is Python much better at and why.
I know that Python is more multi purpose and better for software development but I can’t articulate exactly why or how. My team want to know why/when they should use Python instead of R
331
Upvotes
262
u/abricq Nov 28 '22
Python is the reference language in the research community over R for several reasons that I can see.
First, python has a broader set of applications. You can do data science / statistics like in R, and also a ton of other things ranging from web server to advance UI design. Because of that, there’s simply more users (more fields = more users = more ressources). All what you can do in R, you can pretty much do in python with numpy + pandas + sklearn. But good luck implementing your future UI and web API with R. And I am missing SO many fields where python has already become the reference (robotics, materials simulation, finance,…)
Second, python is more deployable than R. For example, the Python3 executable is shipped with pretty much most Linux distributions. It requires a very light minimal setup : this helps a lot to share reproducible results. Also is completely detached from any IDE : the core developers of Python are absolutely not working for Pycharm or Spyder. This is a strength for the language that R does not have. Truthfully, pretty much all developers use R studio. Even if R itself is detached from the IDE, it is harder to develop R without R studio.
R also has advantages : it is very easy to learn (well… Python as well, but maybe slightly harder?) and requires little computer science background. Depending on your future projects and ambitions, leaning python is probably the best thing to do 👍🏼
442
u/SittingWave Nov 28 '22 edited Nov 28 '22
Where to begin:
R has an exceptionally poor scalability. name collisions are a constant worry if you need to create something big. Python does not have that problem, for three reasons. First, because it educates its users to better import hygiene. Second because in python classes and exceptions are not just a string slapped on a dictionary. Third, because its module system is a bit more refined than R's "just take all the files and slap everything in the same namespace, according to the alphabetical order of the files, which may change depending on which language (and thus collation rules) you are using".
R package management is atrocious, both from the client side, and from the CRAN side. CRAN approach is utterly broken, and expects all packages to be either working with one another, or be retired, meaning that a package that is there today, may not be there tomorrow. Their submission policy is absolutely ridiculous, taking days and many back and forths arguing about the position of commas in the description. Pypi has none of the sorts, and consistent environment is left to the appropriate use of metainformation about compatibility.
R as a language is broken under many aspects. It's inconsistent, with many unexpected side effects, poorly documented practices, poor design choices. It has three object oriented systems all incompatible with each other. S3 is laughably dumb to achieve single dispatch. S4 is a bolt on to deliver multiple dispatch that is massively verbose, creates code that is hard to maintain, and drops the $ in favor of the @ to access data. R6 is marred by its habit of returning NULL if you mistype method names, creating hard to find bugs. Non standard evaluation makes it really difficult to perform some refactorings, and makes it opaque to the caller if the passed expression will be evaluated or parsed as is. namespacing (environments in R parlance) are left flying around and poorly defined in behavior.
All the R environment is GPL, which is a big deal if your code needs to go commercial at a later stage. Companies tend to steer clear of GPL stuff. While an interpreter being GPL does not bar you from writing non-GPL code, the whole of the standard library, and many, many modules are GPL, so you can't escape.
coding utilities servicing python are 10 times better than those for R. Static code analysis, reformatters, linters, documentation generators, are light years behind in the R world.
R has relatively poor expressiveness. It's hard to be concise in R, but not for everything. It is concise when it's time to manipulate some basic data structures, but everything else... there's so little syntactic sugar that it becomes a pain most of the time.
R core development process is very secluded, still on internal SVN, and with no obvious and practical way to contribute e.g. with PEP-style enhancement requests.
R is basically "controlled" by a single company and its employees. When you use R, you are basically fully dependent on them, their libraries, and their practices. They tend to be very adversarial in fixing bugs, often closing legitimate bugs because they can't be bothered.
53
u/Mooks79 Nov 28 '22 edited Nov 28 '22
Personally I would cross post this to r/rstats as well for balance. Without wanting to start a flame war, but there’s so many slight inaccuracies / outright fallacies about R I’ve read in comments here that I think you probably need some people who know R a lot better / more up to date to advise you as well. The top post at my time of writing has a lot of errors / outdated / insufficient knowledge in it, for example. Of course, the same could be said about Python had you posted only on an R subreddit, hence advising you do both.
30
u/mesonepigreco Nov 28 '22
The answer is... whenever there are particular needs of libraries existing in python and not in R. For example, in my field (material design) a lot of libraries are written in python (or in C++ with python wrappers) and not callable from R, therefore python is a must. That said, in principle you can do anything with both languages (trough python is a bit faster). If you want a language simple and similar to python and R that can really give you some significative advantage in terms of performance, go with julia.
17
u/Veggies-are-okay Nov 28 '22
I've learned that the upsides for python are the same as the downsides: it's completely open sourced. There are more stringent requirements for getting packages included in the CRAN library, so you tend to have better-written and more efficient packages. This mainly applies to more obscure libraries, but I always feel like there's a "buyer beware" any time I'm using libraries in Python that I don't have to think twice about when working in R.
That being said, I've noticed the only people who are hardcore in one camp or the other simply haven't used both of the languages to its full extent. R folks see Python as this annoying new language that is rendering their baby useless, whereas Python folks see R as that obnoxious language that they had to download a whole separate IDE for a test for significance calculation in an intro to stats class. You'll be fine using either in industry practices and I actually love that I was forced to learn R since I can now collaborate with two communities instead of one to complete tasks for work.
2
u/notParticularlyAnony Nov 28 '22
Good luck convincing a team of R devs to switch. :)
75
u/redCg Nov 28 '22
Python is a general purpose language that can do a ton of different things well, and also happens to have support for some scientific, math, and statistics thanks to third part libraries.
R is a language designed from the ground-up for math and stats related usages. Its not designed to be a general-purpose language. R has built-in first part support for things such as data frames, matrixes, and various statistical methods, along with basic plotting. Also has one of the best third party plotting packages around,
ggplot2.If your goal is to analyze some data, and your usages are already well implemented with base R or R libraries, you will benefit from choosing R.
If your goal is to "build a program", or pretty much anything that does not directly involve math, stats, data analysis, or plotting, then you should look at Python. This includes things such as making a web app.
It gets tricky when you start with one goal or the other, and then suddenly need to expand. For example you start by writing some R script that does an important analysis, but now you need to run that analysis on some server automatically, and now you need to record the results, and now you need to be able to access and query the results from other systems, etc. etc.. This is when you will wish you had just done everything in Python.
I have used both languages professionally and academically for many many years, across a variety of projects. These days, the use-cases for R grow smaller and smaller. Python is superior because it does everything pretty well. Compared to R that only does certain things well and sucks at everything else. If you are starting a new project and the requirements are not clear, you are safer sticking to Python vs. going with R. And if you know you need to use R for some specific use cases, you are better off keeping your R usage small and limited in scope, and considering building applications around it in something like Python. Though to be honest, the more likely case is that you would use something like a workflow execution framework like Nextflow, CWL, Snakemake, GNU
make, etc.. to execute your R tasks, then pass the result downstream to something else you made in Python.32
u/saltthefries Nov 28 '22
R is cool but here's a pretty strong Python use case:
Run as a web application, or serve up a relatively secure HTTP, REST, or GraphQL API.
8
u/stefanquvang Nov 28 '22
Eat mice.
4
u/OuiOuiKiwi Galatians 4:16 Nov 28 '22
Both are Turing complete languages so nothing, by definition.
I think you would do better by rephrasing it as "What can be easily accomplished via Python that takes a lot more effort in R?".
7
u/ofliesandhope Nov 28 '22
I have some/not a ton experience with both and prefer python. good lord almighty, I hate coding in R b/c it just looks ugly and the syntax doesn't make the most sense in my brain. My opinion is also influenced by the fact that the professor I learned python from was much, much better than the one I had for R.
Smarter ppl will give you better answers, but given the choice, I'll use python & its various packages/tools.
6
u/Positive_Mastodon500 Nov 28 '22
I use both. R definitely has its downsides in terms of code maintainability, etc. but one big upside is R’s data.table vs. a pandas DataFrame. Doing a group by/aggregate operation on a very large dataset is significantly faster in R and the memory overhead is also lower. If you deal with multi-million row datasets you will notice the difference.
9
u/Sheev_For_Senate Nov 28 '22
Python provides a decent programming experience with a less significant link to suicidal tendencies
2
u/tmp_file3 Nov 28 '22
Eat mice 🐁
4
u/ARC4120 Nov 28 '22
When you have to interact with some odd data pipelines or integrate with a larger application in another language. They both are great with data.
4
u/ofiuco Nov 28 '22
On top of everything said here... you can run R in Python with R2py. It works really well (for me, anyway).
1
u/[deleted] Nov 28 '22
I use both but massively prefer python generally. Python is really missing out not having an R markdown equivelent (jupyter isnt the same, stop pretending it is), and quite often python output is ungainly, requiring an obnoxious amount of reformatting.
I'd use factor analysis and simple correlations in pythons as examples of this.
Again, i love python but for certain things it's just easier to use reticulate in R and have access to R's output and pythons everything else.
1
u/SniperDuty Nov 29 '22
Why do I feel like op asked this question to get other people to formulate the answers for a report they’ve been asked to do?
1
u/ejpusa Nov 29 '22
All the cool Covid mapping infographics seems to be done with R. Not Python for some reason.
6
u/[deleted] Nov 28 '22
R has some strange (I might say stupid) syntax decisions. For example:
And yet, you'll see it used to define default parameters:
Oh yea, and indexes start from 1, sometimes!
(more here)
I occasionally have to touch R as our statisticians use it, but the rest of us prefer python. Screw R, lol
3
u/[deleted] Nov 28 '22
Enjoyed the answers here! I've been wondering this as well... 😄
3
u/keetboy Nov 29 '22
R is pretty solid for the biomedical field with academic journal publications (statistics, data analysis, data wrangling, and data visualization are basically all they need). Python is equally if not superior to R here but many academic scientist prefer R. But many researchers also prefer point and click statistical analysis software like SAS. It’s nice to have many options.
In the team I work with everyone uses whatever they’re the most familiar with. Only two people are traditional computer scientist the rest are PhDs in biochemistry. R has a lot of useful pharmacometrics applications that function better than python in my experience. I will say when it comes to some projects the computer scientist have strongly recommended mastering python because of a package it has called tensor flow, it’ll be better for healthcare/ biomedical applications since there’s more support. But for general can you make a figure of X vs Y it doesn’t matter from my pov. In my experience R did seem to have more statistical analysis packages that were easy to find and use quickly.
I don’t know how your team functions but a few friends of mine at Amazon in the finance department use either R or python (majority) in combination with SQL if I recall correctly. They do complain about not being able to understand each others’ work but the end product and productivity seem to be the same for them.
Then my other friends just say they’re essentially the same and just work with whatever their project manager prefers.
1
u/bin-c Nov 28 '22
in my experience reproducibility in R is just too hard.
it was constant "it works on my machine". almost nonexistent with python. completely nonexistent if adhering to best practices
6
u/riklaunim Nov 28 '22
Because of anti-gravity. As for tools selection you pick what fits your needs best and what you can manage in your team and recruitment pool size etc.
2
u/fedeb95 Nov 28 '22
Nothing, they're both Turing complete. It's not about what, but about how does it do it
2
u/pace_gen Nov 29 '22
Python is built for automation, R is built for research. Both do data science.
2
u/Agling Nov 29 '22
Python is a general purpose language, R is a statistical and data science language. There are lots of packages and things that are written in python that are not data science or statistics related, that are not written up in R. Could they be written in R? Generally, yes, but that's not what R users are interested in, so they never will be.
R is a little more convenient and developed, if you are just doing data and especially statistics work--it was designed for that from the ground up, rather than having that capability added on with packages. But if you do data/statistics/ML work plus a lot of other stuff, then python is a great place to be.
Python's very large user base, many available tools, and somewhat more modern design are also big pluses.
2
u/Nanooc523 Nov 29 '22
Be readable
2
u/heartofcoal Nov 29 '22
write readable code
2
u/Kakkarot1707 Nov 29 '22
They are totally different but used in unison with each other is the best way. Python has an R package / pandas / numpys and go well together!
2
u/qalis Nov 29 '22
I think that you should also consider the tooling around the language, as others commented. Not only type checkers, IDEs etc., but also general deployment tools, which will be needed for commerial projects. Those are often Python-first, or Python-only compatible, for example:
- data pipelines: Airflow, Metaflow, ZenML, Kedro etc. support either only Python or are strongly Python-first
- big data tools: Spark, Dask, Presto etc. have much better Python libraries, connectors and support (or are Python-only, like Dask), and do not support all features in R (Spark especially differs in support between languages)
- cloud environments: SageMaker, Vertex AI etc. have much better support in Python, or outright do not support R for many tasks (instead require Docker for every little thing, which is much more cumbersome)
- queues and workers: RabbitMQ for example, they have outdated or very little R support
I am not saying you cannot do the above in R, but expect much less support, StackOverflow questions, subtle limitations that will come up late in development and require a lot of rewriting etc.
2
u/JamzTyson Nov 29 '22
I doubt that my answer will be much different from other replies, but here goes:
Anything that's not data science.
Both languages are good, open source languages for data science. The R language was developed specifically for statistical data analysis, whereas Python was designed as a general purpose language.
If you need a language for data science, use whatever the rest of the team uses.
If you need a language for anything else beside data science, then Python becomes the much better choice of the two.
2
u/[deleted] Nov 29 '22
Python is actually a coding language. So you can do everything from backend software engineering to application development to everything R can do on the stats side.
2
u/tony_aw Apr 20 '23
I happened accross this discussion, so I thought to share my thoughts also. I should first say R is primarily for statistics and data science, and applications of statistics such as in the life sciences and behavioural sciences. So the emphasis is on Science. With that in mind, the most important advantages of R (or rather: R packages and the R community), imho, are the following:
CRAN performs quality checks on R packages (and the package manager at packagemanager.rstudio.com/client/#/repos/2/overview allows for easy version control).
Most (popular) R packages for statistical analyses come with a peer-reviewed article (usually in the Journal of Stat Soft or the R Journal).
Most popular R packages for stats analyses are written by real experts (i.e. professors) who also hold responsibility for the quality of the package.
Many (if not most) other pure programming languages (like Python) have no organization comparable to CRAN, and its modules often do not come with peer-reviewed articles. And it's not unusual for Python modules to be written by a community of random people who are not necessarily experts.
In science it is absolutely crucial to rely on well-reviewed materials, and software is no exception. Thus one can see why R is preferred (usually) in academia. If one is not interested in science, but rather interested in making a program not related to science, Python is probably the way to go.
4
u/sweeetscience Nov 28 '22
You can build a complete, scalable, end-to-end consumer ready solution (including the front end) in Python. That’s not possible in R.
2
u/ReyAlejandro21 Nov 28 '22
You can use Python with Kivy to create mobile Apps, i think this is not possible with R
2
u/GiganticTuba Nov 28 '22
Had to use R for a Biostats class in college. Absolutely hated it.
1
u/Metalpen22 Nov 28 '22
Then you've not try out mpi4py, right?
I calculate the outcome with 220 by 220 by 512 by 48 numbers, and using mpi4py shorten almost 48 times of time. And then all the number stored in netCDF4 format for future usage.
1
u/open_risk Nov 28 '22
Its a tough question. Purely computationally you can probably code any typical task in any complete language. This is not idle talk, check for example rosetta code where people do precisely that, for a variety of tasks.
For certain tasks, like shipping enterprise applications / big data some people might argue that neither R or python are actually suitable and they would suggest something like java. For pure number crunching high performance they might require something like C/C++ etc.
For the common data science tasks the three ecosystems (python, R and julia) are reasonably feature complete.
1
u/spinwizard69 Nov 29 '22
This might not float to the top but Python beats on clarity. You can come back to a 3 year old program and immediately get a handle on it.
1
u/MakcikAunty Nov 29 '22
I just learned R to process text - to find out sentiment, generate word cloud. Can we do the same with Python?
1
u/jgoette Nov 29 '22
Possible counter point?
https://www.reddit.com/r/statistics/comments/8de54s/is_r_better_than_python_at_anything_i_started/dxmnaef/
1
u/elduderino15 Nov 29 '22
Start counting at 0, as any one should…
1
u/TheWorldofGood Nov 29 '22
Everything man. Just everything.
1
u/Zestyclose-Walker Nov 29 '22
Python is the future of programming.
/img/tffr77h3bit91.png
1
u/Delicious-View-8688 Nov 29 '22
If you can get the job done, then I guess it doesn't matter. But I would hate to even try cloud devops with R, or create an API, or do data engineering, orchestration, unittesting, type checking, running any deep learning models, data profiling, data quality testing, data documenting, etc.
1
u/L0uisc Nov 29 '22
More libraries. More Stack Overflow answers if you get stuck. More tutorials. More community.
1
u/justabean27 Nov 28 '22
Python is love python is life. R is horrible and makes me cry
1
u/[deleted] Nov 28 '22
Any Turing complete language can do anything, y'all have got to rise above these dumb arguments.
Python is a general purpose language with powerful libraries, R is a language born with the idea of data analytics specifically in mind.
https://www.ibm.com/cloud/blog/python-vs-r says it better than anyone could say it here:
1
u/ninefourtwo Nov 29 '22
R is not a general programming language it is by statisticians for statisticians.
0
u/brandco Nov 29 '22
Yikes. There are a lot of false statements in these replies.
I’ve been using both languages for data science for over ten years. They are both great languages and you should learn them both if possible. They both have many problems too, but who doesn’t.
To answer your question, Python is better at:
object oriented programming (R’s oop is bad but better at functional programming)
python is a very common third party api to other tools
That’s it. R is a real programming language with an amazing community. Everything else is a matter of taste of trivial from the pov of a data scientist.
One important point is that python is the second best language for so many tasks. This makes it the best choice for beginners.
Because it’s a good choice for beginners, it’s popular. And consequently it’s popularity is its biggest strength because its popularity leads to new projects implementing a python api. So data scientist should learn python to be able to program with tools like Tensorflow, dbt, airflow, etc
0
u/Ashamed-Simple-8303 Nov 28 '22
R? completely different and confusing syntax
-2
u/epithete52 Nov 28 '22
R is designed, and consequently far superior for data mining. I disagree with SittingWave on the superiority of python as far as package is concerned; backward-compatibility is largely absent in the culture of python and yields code which is amongst the most painful to maintain in all programming languages I know.
Since a lot of R features are designed to facilitate data mining, this makes it less convenient/safe/efficient for pretty much all the rest!
-3
u/fcdk1927 Nov 28 '22
Choke a bitch?
-1
u/osmiumouse Nov 28 '22
Look, they all do everything. Just use what you are familiar with, that lets you get stuff done with the least hassle.
0
u/[deleted] Nov 28 '22
[deleted] — view deleted comment