Home  >  Article  >  Backend Development  >  Which is better, R or Python?

Which is better, R or Python?

little bottle
little bottleforward
2019-04-09 16:06:593227browse

If you are engaged in the field of data science and mention programming languages, you will immediately think of R language and Python languageIf you search "R vs Python" in Google ", you can see a lot of discussions about which one is better, but in factWhether it is R or Python, both are excellent tools.


Which is better, R or Python?

##One of the reasons for this phenomenon is that users based on The programming language you use divides the field of data science into several camps, including the R camp and the Python camp. People in both camps believe strongly that their chosen language is far better than the other's. So to some extent, this disagreement does not all come from the tool itself, but also from the user.


Why not use both languages ​​at the same time?


In fact, in the data science circle, there are also people who use Python and R at the same time, but the proportion is very small. In addition, a large number of people who insist on using a certain language also hope to master certain functions of another language. For example, R users sometimes need the object-oriented properties of Python. At the same time, some Python users also want to use R's powerful statistical distribution functions.


Which is better, R or Python?##

The chart above shows the results of a survey conducted by Red Monk in the third quarter of 2018. This result is based on the popularity of languages ​​on Stack Overflow and Github, and it’s clear that both R and Python score highly. The language itself is not the reason why we cannot use both in the same project. Our ultimate goal is to do more insightful and valuable analysis, and the choice of language should not be an obstacle in this process.


Looking at R and Python


Below we can first compare these two languages ​​from several different aspects and look at their advantages and disadvantages respectively.


Which is better, R or Python?


  • ##Python


## Since its release in 1991, Python has become extremely popular, especially in data processing. There are several reasons why it is so popular:


  • Object-oriented language

  • Universality

  • Strong scalability, strong community support

  • # #Easy to understand and learn

  • ##Packages such as Pandas, Numpy and Scikit-learn make Python an excellent choice for machine learning


However, unlike R, Python does not have dedicated packages for statistical calculations.


Which is better, R or Python?


  • ##R


## R was first released in 1995 and immediately became one of the most widely used tools in data science.


Contains almost any statistical application toolkit you can think of. Currently CRAN has over 100,000 packages.
has excellent visualization tool packages, such as ggplot2.
Able to conduct independent analysis.
  • In terms of performance, R is not the fastest language, and it may consume a lot of memory when processing large data sets.


    ##"Two swords combined"


    Is it possible to use the extraordinary statistical power of R and the programming power of Python at the same time? We can easily combine SQL code with R or Python to write a script, so why not use R and Python together?


    There are two basic methods for applying Python and R at the same time in the same project. Next, I will share these two methods with you. :


    ##R within Python


    • PypeR


    ## Provided by PypeR A simple way to connect R to Python is through a pipe. PypeR also exists in the Python toolkit, providing a more convenient installation method. PypeR is ideal when frequent data transfers between Python and R are not required. When we run R through pipelines, Python programs are more flexible in terms of subprocess control, memory control, and portability across operating systems, including Windows, GNU Linux, and Mac OS.


    Which is better, R or Python?

    • ##pyRserve


    ##pyRserve Using

    Rserve As the RPC connection gateway, through this connection method, users can set R variables in Python, and R functions can also be called remotely.


    • ##rpy2


    ryp2 can run the embedded R language in the Python process. It creates a framework that converts Python objects into R objects, passes them into R functions, and finally passes the R output back to Python objects.


    Which is better, R or Python?#

    One of the advantages of calling the R language in Python is that we can easily use the powerful tool packages of the R language in Python, such as ggplot2, tidyr, dplyr, etc. Let's take a simple example to see how to use ggplot2 in Python.


    • ##Basic Plot


    Which is better, R or Python? https://rpy2.github.io/doc/latest/html/graphics .html#plot


    Which is better, R or Python?

    • #Geometry


    Which is better, R or Python?##https://rpy2.github.io/doc/latest/html/graphics.html#geometry


    Which is better, R or Python?

         

    关于 rpy2 的资料

    rpy2官方文档

    https://rpy2.github.io/doc/latest/html/index.html

    RPy2: Combining the Power of R Python for Data Science:

    https://community.alteryx.com/t5/Data-Science-Blog/RPy2-Combining-the-Power-of-R-Python-for-Data-Science/ba-p/138432

    Accessing R from Python using RPy2:

    https://www.r-bloggers.com/accessing-r-from-python-using-rpy2/


    Python with R


    We can run R language scripts in Python through the following tools:


    • rJython


    This toolkit deploys an interface to connect Python through Jython, providing a way to connect Python with R connected interface.


    • ##rPython


    rPython is also a toolkit for calling Python in R language. It can run Python code in R, call functions, allocate and obtain variables, etc.


    • ##SnakeCharmR


    SnakeCharmR

    is an improved version of rPython, which is derived from rPython's 'jsonlite' branch, which has many improvements compared to rPython.


    • PythonInR


    PythonInR allows users to use Python in R language very conveniently and provides functions that can interact with Python.


    • ##reticulate


    The Reticulate package provides a complete set of tools that enable Python and R to interoperate. Of all the tools mentioned above, this one is the most widely used, mostly because it was developed under the leadership of Rstudio. Reticulate embeds Python modules within R modules, allowing the two languages ​​to interoperate seamlessly and with high performance. Using this toolkit, we can "weave" Python code into R, creating a new type of project that combines the two languages.


    Which is better, R or Python?


    # #Reticulate package provides the following functions:


    • Provides multiple ways to call Python in R language, including R Markdown , import Python scripts, import Python modules, and use Python in R modules.

    • Provides conversion between R objects and Python objects (for example, conversion between R and Pandas data frames, or R matrix and NumPy array conversion).

    • Flexible cooperation with different versions of Python, including virtual environments and Conda environments.


    Resources about the reticulate toolkit:

    The The documentation provides many examples to get you started:

    https://rstudio.github.io/reticulate/

    Use the reticulate package, using Python in R:

    ##https://longhowlam.wordpress.com/2017/04 /10/test-driving-python-integration-in-r-using-the-reticulate-package/

    "snake" in the tool package: use reticulate connects Python with R:

    https://www.mango-solutions.com/blog/snakes-in-a-package-combining-python -and-r-with-reticulate


    #Conclusion


    #R and Python are both complete and stable languages, and both are sufficient to complete a data analysis task. Although both have their own advantages and disadvantages, if we can make good use of their advantages, we can complete the task better. In short, knowing two languages ​​at the same time makes us more flexible and able to work comfortably in a variety of environments.

    [Recommended course: Python video tutorial]

The above is the detailed content of Which is better, R or Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete