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

36

u/akcom Apr 19 '18

Pandas is also 2-10x slower than R data.table for most common data tasks.

7

u/bubbles212 Apr 19 '18

How do pandas and dplyr compare in terms of computation speed? I know data.table is the go-to for speed in R, but I prefer dplyr's syntax when computation times aren't a limiting factor.

12

u/akcom Apr 19 '18

dplyr and pandas performance are broadly similar (same order of magnitude)

4

u/geosoco Apr 19 '18

FWIW, I think this depends on a number of things. I think if you use cython or JIT-ing through numba and such.

It probably also depends on which the platform and how you're getting pandas too as some distros fail to include some of the optimized paths.

Usually I'm using R because of some specific libraries or for graphing, which is still much nicer than in python (though python has been improving a lot recently).

When I use R, it's often for specific libraries or for graphing as I find it's still far easier to make pretty graphs with ggplot2 and friends which more than make up for other shortcomings, especially if i cache results from longer computations.

6

u/bjorneylol Apr 19 '18 edited Apr 20 '18

If pandas is slower for you, then you are probably using it sub-optimally - I just ran some benchmarks and pandas was about 2.5x faster on a single column groupby and sum, and about 12x faster on subsetting (I reran this before with better R code below and they are basically equivalent now) . R treats strings as categorical data by default, whereas with pandas you need to specify that you want to do this as it will otherwise leave them as strings. You can't say R is faster than python if you are using an optimized R solution (data.tables) but not the equivalent python solution.

Granted my R isn't great and I'm unfamiliar with data.tables so if i'm doing it wrong let me know.

Data:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 723778 entries, 0 to 723777
Data columns (total 9 columns):
Dept           722911 non-null object
Sub Dept       722738 non-null object
Vendor         723728 non-null object
Model          586510 non-null object
Description    723361 non-null object
Year           723778 non-null int64
Qty Sold       723778 non-null int64
Sales Total    723778 non-null float64
Cost Total     723778 non-null float64
dtypes: float64(2), int64(2), object(5)
memory usage: 351.2 MB

R:

dt = data.table::fread("temp/Report-6io.csv")
# 1.19 seconds with data.table, 10.6 seconds with vanilla dataframe
object.size(dt)
# 87 mb with data.tables, 77mb with vanilla dataframe

sub <- dt[Dept == dept]
# 0.5 seconds 

sub <- dt[, sum(dt$`Sales Total`), by = Dept]
# 0.78 seconds

Pandas:

df = pd.read_csv("temp/Report-6io.csv", encoding="ANSI")
# 1.41 seconds, 350MB in memory

for col in df.columns:
    if not df[col].dtype in (float, int):
        df[col] = df[col].astype('category')
# another 1.3 seconds, reduces memory usage to 107.7 MB

for dept in df["Dept"].unique():
    sub = df[df['Dept'] == dept]
# 1.11 seconds vs 43 seconds with object dtype

for i in range(100):
    grp = df.groupby(by="Dept").agg({"Sales Total":np.sum})
# 0.53 seconds vs 4.2 seconds with object dtype

18

u/[deleted] Apr 19 '18

Why would you choose to use the subset function over data.table if it has it's own very fast subsetting in i? What is the point of doing a comparison if you invested no time in understanding the tool? :o

If you want fast subsetting in data.table you should set key beforehand with setkey, if not it should be slower on the first iteration but IIRC it caches the subset for subsequent ones.

Moreover - looping for testing is a nono, you have special libraries for that with proper timing tools - ie microbenchmark.

I did a quick test on made up data and it doesn't align with what you wrote.

temp <- data.table(A = 1:10, B = runif(1e5))
setkey(temp, A)
> microbenchmark::microbenchmark(temp[, sum(B), by = A], 
temp[, sum(temp$B), by = A], aggregate(B ~ A, temp, sum))
Unit: milliseconds
                        expr       min        lq      mean    median        uq        max neval cld
temp[, sum(B), by = A]  1.403814  1.457962  1.715963  1.497370  1.656556   9.074206   100  a 
temp[, sum(temp$B), by = A]  2.030006  2.069880  2.200319  2.132716  2.272973   4.776812   100  a 
aggregate(B ~ A, temp, sum) 69.642354 72.708928 86.480925 73.444192 76.104426 239.075531   100   b

If you want to do a test that has any sensibility in it please learn the tool so you are using it properly and also please don't go for loops as default in R, it's not idiomatic.

1

u/afatsumcha Apr 19 '18 edited Jul 15 '24

humorous shocking frame normal shaggy smart act grandiose jellyfish ask

This post was mass deleted and anonymized with Redact

1

u/[deleted] Apr 20 '18

As long as you don't need the speed then tidyverse is very nice.

1

u/[deleted] Sep 23 '18

I'd also add that tidyverse is a square shit to use in production (if you ever need to deploy your code in something like AWS)

1

u/bjorneylol Apr 20 '18

I reran these in a different post below - yeah the R code wasn't optimal, so i fixed it and they are now essentially equivalent. https://www.reddit.com/r/statistics/comments/8de54s/is_r_better_than_python_at_anything_i_started/dxnhr80/

Regarding loops - you are right, it isn't optimal, but its a good enough approximation when you are trying to catch order of magnitude differences. When you properly benchmark the pandas code it is up to 4x faster than running in a loop in ipython because it doesn't have the gc overhead.

Both tools are basically the same C code under the hood, the only difference is language and class overhead. Performance on either could probably be increased substantially by compiling everything from source code with hardware optimizations rather than using the downloadable binaries (which I guarantee very few people actually do).

6

u/EffectSizeQueen Apr 19 '18 edited Apr 19 '18

You have a few issues. Fairly certain that subset.data.table is going to be slower than doing dt[Dept == dept]. Not sure by how much, but I'm seeing a pretty substantially difference on a dataset I have loaded. Also, explicitly looping through the groupings in R like that isn't idiomatic data.table, and is almost certainly a big performance sink. I can't think of an obvious and frequent use case where you wouldn't just let data.table iterate through the groups internally.

The range function doesn't operate the same way it does in Python — range(100) returns c(100, 100), so you're just looping through twice — seq(100) gets you what you're after. Kind of confused about the numbers you're giving there, considering you're iterating 100 times in Python and only twice in R.

In terms of benchmarks, I haven't seen anyone really poke holes in these, from here, or [these] (https://github.com/Rdatatable/data.table/wiki/Benchmarks-:-Grouping). Both show data.table being faster.

Edit: forgot to mention that using the $ operator inside the aggregation is unnecessary and also quite a bit slower.

3

u/bjorneylol Apr 20 '18

Thanks for the tips, had no idea about range. Removing the $ operator in the aggregation really did speed things up substantially on the groupby

What I'm seeing now is basically equivalent performance when working with pandas categories. I know at least that last set of benchmarks you posted are using 0.14, and I can certainly say pandas has come a long way since them (0.22 four years later). When you get down to the metal data.tables and pandas are likely using slightly different implementations of the same C algorithms for all their subsetting/joining, and any speed difference is likely due to overhead in the dataframe/table classes and/or the language. I haven't tested merges and sorts, but I wouldn't be surprised it would be similar performance along an int64 index, with R outperforming on text data (Last time I checked, pandas converts categorical columns back to strings for a lot of operations, so the conversion to or from would kill speed).

The dt[x==y] syntax is a lot faster

microbenchmark::microbenchmark(sub <- dt[Dept == "XYZ"])
# 4.2 ms
microbenchmark::microbenchmark(sub <- subset(dt, Dept == "XYZ"))
# 8.8 ms (mean was 9.0)

#python
timeit.Timer('sub = df[df["Dept"]=="XYZ"]', 'from __main__ import setup_data; df=setup_data()').repeat(5,10)
# 3.2 ms as category, 48ms as string

Similarly removing the $ operator speeds up the groupby a LOT

microbenchmark::microbenchmark(sub <- dt[, sum(`Sales Total`), by = Dept])
# 5.4 ms (vs 680ms with the dt$`Sales Total` syntax)

#python
timeit.Timer('sub = df.groupby(by=["Dept"]).agg({"Sales Total":"sum"})', 'from __main__ import setup_data; df=setup_data()').repeat(5,10)
# 5.1 ms as category 42ms as string
→ More replies (1)

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

→ More replies (1)

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.

1

u/[deleted] Apr 19 '18

is data.table particularly fast? Because I remember reading that Pandas was actually faster than base R dataframe manipulation.

3

u/[deleted] Apr 20 '18

Yup data.table is very fast.

2

u/bjorneylol Apr 19 '18

data.table is faster than the base R dataframe.

Pandas is substantially faster than the base R dataframe, and depending on usage, faster than data.table

https://www.reddit.com/r/statistics/comments/8de54s/is_r_better_than_python_at_anything_i_started/dxn9rl0/

0

u/[deleted] Apr 19 '18

[deleted] — view deleted comment

3

u/akcom Apr 20 '18

The data.table code used in the statworx is so bad I wouldn't even call it unoptimized. It's as if someone who never used data.table before picked it up without reading the documentation and tried to write code.

Second one isn't as bad, but still clearly un-optimized.

Of course performance is a moving target. But by your logic, we can never compare the performance of things that are changing, which seems myopic.

1

u/Pfohlol Apr 20 '18

This might be a weird thing to do, but do you have any idea if data.table called from Python with rpy2 retains the speed benefits? I do most of my ML in Python but I do my data exploration and preparation in R due to the speed of data.table. It may be easier overall if I could put everything in one language