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

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).