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

100

u/marrone12 Apr 19 '18

Honestly pandas has a terribly obtuse syntax but python is much better programming language for everything besides statistical analysis. I mostly code in python out of necessity but data analysis itself is much better in R.

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.

→ More replies (4)

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)
→ More replies (2)
→ More replies (1)
→ More replies (6)

10

u/nsfy33 Apr 19 '18 edited Mar 07 '19

[deleted]

15

u/marrone12 Apr 19 '18

I started learning R with plyr nearly 10 years ago. Dplyr came out more recently.

As far as intuitive, pandas vs R for simple things like subsetting: Python: data.loc[data['id']==25822] Vs R: filter(data, id == 25822)

I feel like R's syntax is easier to understand for someone coming from excel. But to each their own.

7

u/tsunamisurfer Apr 19 '18

Even simpler with data.table: data[id==25882]

5

u/nsfy33 Apr 19 '18 edited Mar 07 '19

[deleted]

4

u/marrone12 Apr 19 '18

The way I did it five years ago was to do subset(data, id == 25822)

2

u/[deleted] Apr 19 '18 edited Apr 19 '18

You could do something like this 10 years ago (and probably more):

iris ->.
  subset(., Species == "virginica") ->.
  subset(., select=c("Sepal.Width", "Sepal.Length")) ->.
  colMeans(.)

all in base R.

2

u/dialecticalmonism Apr 19 '18

In Pandas there are multiple ways to accomplish the same thing. In this case, you could just use:

data[data.id == 25822]

2

u/marrone12 Apr 19 '18

Had no idea that worked. All the official documentation and stack overflow always uses the ix and loc methods.

3

u/dialecticalmonism Apr 19 '18

Yeah, the fact that you can do things in multiple ways within Pandas definitely cuts both ways.

3

u/figurative1y Apr 19 '18

You can also use pipes:

data %>% filter(id == 25822)

3

u/marrone12 Apr 19 '18

Yeah I'm aware of pipes just wanted to keep it simple for the example

→ More replies (3)

6

u/feelphree Apr 19 '18 edited Apr 19 '18

I agree. I also came to Python from R and find the entire Python DS stack much more intuitive. Especially since the growth of the Hadleyverse.

→ More replies (10)