r/statistics • • Apr 19 '18

Software Is R better than Python at anything? I started learning R half a year ago and I wonder if I should switch.

I had an R class and enjoyed the tool quite a bit which is why I dug my teeth a bit deeper into it, furthering my knowledge past the class's requirements. I've done some research on data science and apparently Python seems to be growing faster in the industry and in academia alike. I wonder if I should stop sinking any more time into R and just learn Python instead? Is there a proper GGplot alternative in Python? The entire Tidyverse package is quite useful really. Does Python match that? Will my R knowledge help me pick up Python faster?

Does it make sense to keep up with both?

Thanks in advance!

EDIT: Thanks everyone! I will stick with R because I really enjoy it and y'all made a great case as to why it's worthwhile. I'll dig into Python down the line.

140 Upvotes

153 comments sorted by

View all comments

Show parent comment

4

u/backgammon_no Apr 19 '18 edited Mar 11 '25

sort fuzzy outgoing sharp rob chase follow hospital waiting whistle

This post was mass deleted and anonymized with Redact

6

u/akcom Apr 19 '18

Absolutely. I found this especially painful for very large datasets. We would either drop to numpy or do it data.table. In my experience, numpy was really unintuitive for things like grouping and operations on groups. But I'm not a numpy expert so that might part of the problem. A couple examples:

https://stackoverflow.com/questions/47098571/most-efficient-way-to-groupby-aggregate-for-large-dataframe-in-pandas (see the numpy solution)

https://stackoverflow.com/questions/47125697/concatenate-range-arrays-given-start-stop-numbers-in-a-vectorized-way-numpy

https://stackoverflow.com/questions/47115448/pandas-get-index-of-first-and-last-element-by-group

5

u/bjorneylol Apr 19 '18

I commented above, but I believe pandas is still much faster provided you downcast string arrays to indexed categories (which R does by default).

Pandas benchmarks 2-10x faster for me

2

u/akcom Apr 20 '18

What sort of operations are you performing? I found pandas to be substantially slower on groupby(), calculating aggregates, and most importantly join/merge

1

u/bjorneylol Apr 20 '18

I revised my numbers, my R code was apparently not optimal (https://www.reddit.com/r/statistics/comments/8de54s/is_r_better_than_python_at_anything_i_started/dxnhr80/)

From what I'm seeing now they are basically equivalent. I think there are going to be edge cases where one outperforms the other, but a lot of the speed will come down to things I'm not willing to test (I'm using the windows binaries, I'm sure if I compiled everything from source to take advantage of hardware optimizations the results may be drastically different)

I haven't tested join/merge but I would assume they are similar if you perform them in the optimal way (along an integer axis is pandas) - i'm not sure how the pandas categorical types handle manipulation (I think they convert back to string) so I wouldn't be surprised if R outperforms in that regard.

1

u/backgammon_no Apr 20 '18

Is this like R's factors?

My data is usually unique, not factorizable.

2

u/bjorneylol Apr 20 '18

Yeah, pandas categorical type is similar to R's factor (integers under the hood, but displays the associated string data).

If its unique data there is no point in using pandas categories, I'm not sure if R converts to string as well? In either case I assume you aren't doing a ton of subsetting/grouping/joining on unique string columns

1

u/[deleted] Sep 23 '18

Oh jesus. I was at a place that insisted on pandas for a while. Pandas isn't native to Python so the problem you'll run into is that it's incompatible with a lot of Python, unlike R where vectors and dataframes are core native data types that work with everything.
Seriously, Pandas is not a replacement for R. It's nice when you have a Python project that needs to add a bit of data analysis on the side, but it's simply not designed to be a primarily analysis language.