[Reprint] r dancing with Python

Source: Internet
Author: User
Tags python list
ArticleDirectory
    • Understanding R instances
    • Objects accessing R
    • Call the r Function
    • An R instance is an R console.
    • Load custom functions
    • R vector and Python list
    • No end
Http://www.dataguru.cn/article-1807-1.html makes R dance with Python

2012-7-19 20:50|Publisher: xiandou|View: 282|Comment: 0|Author: shouzhuantu|From: Sina Blog

Summary : R (also known as R language) is an open-source cross-platform numerical statistics and numerical graphical display tool. In general, R is used for statistics and drawing. R has its own scripting language and a large number of statistical and graphic libraries (thanks to the open-source Community ), Which makes her look both beautiful and practical. Similar software (such as S...

R (also known as R language) is an open-source cross-platform numerical statistics and numerical graphical display tool. In general, R is used for statistics and drawing. R has its own scripting language and a large number of statistical and graphic libraries (thanks to the open-source community), which makes her look both beautiful and practical. Compared with other similar software (such as SPSS), R is characterized by pure command line, which means we should focus more on the data itself, rather than the UI of the statistical tool.

Although R has a set of language, it is quite complete, but she is the most professional in statistics and drawing, other languages are responsible for connecting to databases, text processing, file operations, and other dirty jobs, my choice is the best Python that we are most familiar with and that is the best to do these dirty jobs. Then the question is very clear. How does R and Python work together? Pat your head and think of the following methods:

1. R and Python only share files. Python processes the source data cleanly, generates formatted files, and stores them in a pre-defined directory. A timer allows R to read files and finally Outputs Statistical results and charts.
To a certain extent, this method can be used, in addition to timer, but also allows python to instantly execute the "rscript" command to call the r script to work, but this method is too restrictive and only supports file exchange, python cannot precisely control R.

2. Let Python directly call R functions. R is an open-source project, and there will certainly be some third-party libraries for python to communicate with R.
Indeed, I found rpy2, which can be used to read R objects, call R methods, and convert the data structures between Python and R. In fact, in addition to Python, third-party packages for interconnection between other languages and R are also large.

Finally, I chose 2nd methods to let R dance with python.

Rpy2.robjects is an advanced R encapsulation of rpy2. This module contains an R object and a series of R data structures. In most cases, you only need to deal with this module. The installation of rpy2 is not mentioned here. If you are interested, read the document and try to see how R can be seamlessly integrated with python.

Understanding the r instance of an R instance refers to rpy2.robjects. R, which is an embedded r process in Python. You can use R as a channel from Python to R. Through the r instance, we can read R's built-in variables, call R functions, and even directly use it as the r parser. The object accessing R is in the command line of R. We directly enter the object name to access the built-in objects of R, such as PI and letters: Access the r object in the r Console Using the r instance, python is also very easy to access the r object, and there are many methods: Access the r object in Python In this section Code We use three methods to access the r object, take the r instance as a dictionary, take the r instance as a method, and treat the r instance as a Class Object (which is really a metaphor ~). In practice, which of the following methods should be used differently? The third method I like is to use the r instance as your own and directly use "." To access the r object. However, this method has a defect, that is, it cannot access R objects or functions with namespaces. The other two methods are acceptable, which will be described later. By calling the r function through the R instance, we can easily call the r function using python. Next we will read a data file on the R console and Python command line and draw a dot chart. Figure of reading a file on the R Console Code explanation: the content of data.csv is the content from 3 to 7 of the above Code. Data = read.table('data.csv '): Read the file into a data box variable data. (Delete. Dotchart (SVD) uses matrix data to draw a dot chart. The result is as follows: Next we will use python to do the same thing. We have learned that using the R instance can directly access the r object and directly call the r function. In Python's view, objects and functions are the same thing, and functions are also an object. Now, try again and use read.table()" to read a data file data.csv: Error! What's going on? As I mentioned above, the referenced method cannot access R objects and functions with namespaces. Read. table indicates the table function under the read package ". the call fails in the form of ", which must be obtained in dictionary or parameter mode: the result of this Code is the same as that of drawing a dot chart on the R console. In the last line, the R. dotchart (CTX) directly calls the r function dotchart through ".". It is normal if there is no real-name space. If you want to avoid too many uncontrollable error opportunities, you can use the dictionary to access R objects and methods in a unified manner. This is the safest way, although I personally think it looks awkward. An R instance is an R console.

In fact, the R instance is an interactive R console, but the interaction object is Python and R. To prove that the R instance has the characteristics of the r console, we can make an experiment and write a series of R scripts, as the content of a string variable in Python, pass the string to the r instance and call the r instance as a method:

The result is as follows:

Note: The R instance can only be used in R (r code) mode to use the r instance as the console. The dictionary method does not work.

Load custom functions

In practical applications, writing your own functions using the R language is also inevitable. In the r console, you can use source ('script _ path ') to load the custom R script. In python, using functions in your own R script is also convenient: using R. source ('script _ path') to load the UDF to the global environment, and then use R. the custom method name can be called. I did this and I will not detail it here. Let's do it on your own.

R vector and Python list

Vector is the most important and commonly used data type of R. It can be understood as a two-dimensional data, corresponding to the python list. On the r console, declare a variable "x <-1". X is declared as a vector, and its first value is 1. R often uses the C () function to create a vector composed of multiple values, such as C ). Python must deal with R. In addition to accessing R objects and calling R functions, it also needs to learn how to convert common data types.

Rpy2 provides several classes for us to convert the python list to the R vector. They are robjects. intvector, robjects. boolvector, robjects. stringvector, robjects. floatvector. taking intvector as an example, convert Python list to R vector: robjects. intvector ([1, 2, 3, 4, 5]), bi!

Next we will use the type conversion knowledge we just learned to draw a scatter chart of the previous example to end this experience:

No end

Rpy2 provides more than just the above. The above knowledge is only 20% provided by rpy2, but it is enough to solve the problem of 80%. Rpy2 also provides lower-level APIs for you to do more things. For example, you can implement another robjects object to support "." To access objects and functions with namespaces. For more information, see the official documentation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.