Overview of the R language

Source: Internet
Author: User

R is a software system with statistical analysis and powerful graphing capabilities, and is co-founded by Ross Ihaka and Robert Gentleman. It is a free, free, open source software that belongs to the GNU system, and is also an excellent tool for statistical computing and statistical mapping at the same time.

I. Development history of R
To say R. You have to say the S language first.

About 1980 years. The T-Bell Laboratory has designed an S language widely used in the field of statistics.

s language is an interpretive language. Designed to be used for data exploration, statistical analysis and mapping.

The initial implementation version number of the S language is mainly s-plus. It is a business software based on S language, which is further intact by the Department of Statistics Science of Mathsoft Corporation.

Later. Robert Gentleman of Auckland University and Ross Ihaka and other volunteers developed an R system.

R is a GNU project based on the S language. So it can also be used as an implementation of the S language. In other words, the code written in the S language can be executed without modification in the R environment.

Because of this, some people say that R is a "clone" of S-plus.

Ii. main functions of R
R is not only a software, but also a language, it is a complete set of data processing, computing and mapping software system.

The main functions of R include: data storage and processing, array operation, statistical analysis, statistical mapping and so on.

In addition, it provides a simple and powerful programming language that manipulates the input and output of data, enables branching, looping, and enables user-defined functions.

third, the download and installation of R
The installation files for R support multiple platforms. That is, we can use R on Windows,linux or Mac OS x.

The download URL for the R installation package is: https://cran.r-project.org/. Click the link. The ability to enter 1 of the pages you see.


Figure 1 Display page of the R installation package

As we can see from Figure 1. R's installation package supports Windows,linux and Mac OS x three platforms. You can choose the appropriate installation package based on the system you are using.

The author uses the Windows system, so click on "Download R for Windows" in Figure 1 to see the 2 pages you see.


Figure 2 Overview of the R installation package under the Windows platform

As you can see from Figure 2, the installation package for R is divided into the base package (base), the contribution package (contrib), and the Toolkit (rtools).

For those who have just started learning, it is enough to use the base package.

When you want to learn more about R, download the contribution Pack (contrib) and Toolkit (Rtools).

Click "Base" in Figure 2. Go to 3 to see the download page.


Figure 3 download page for the R installation package

Click "Download r 3.2.3 for Windows" in Figure 3 to download the basic installation package for R.

After the installation package is downloaded. You can start the installation by double-clicking it. Follow the installation instructions step-by-step so that you can install R on your computer and create shortcuts on your desktop at the same time.

First access to the R software interface 4 see.


Figure 4 Interface of the R software

As can be seen from Figure 4, the R default command prompt is ">", which indicates that the command is waiting for input.

Iv. Fundamentals of R
Before we learn r, let's look at how R works.

We know that programming languages like C, C + +, and Java are the times we use them. It is necessary to construct a complete program form that cannot be executed by entering a single command. But R is not this. Because R is an interpreted language. Instead of compiling the language, this means that the input command can be executed directly. Without first constituting a complete procedural form. The syntax of R is also very simple and intuitive.

When R executes, all variables, data, functions, and results are in the active memory of the computer in the form of an object (objects), and are crowned with the corresponding name code. We can manipulate these objects by using some operators, such as arithmetic, logic, comparison, and some functions.

With regard to functions in R, we can graphically describe the narrative in figure 5来.


Figure 5 Functions of R

The parameters in Figure 5 (arguments) may be objects (such as data, equations, calculations, etc.). Some of the parameters are preset as default values in the function. The user can make individual changes as needed, so executing an R function may not need to be set regardless of the number of references. The reason is that all of the parameters can be implicitly default values. It is also possible that the function itself does not contain any of the parameters.

The specific working principle of R 6 is seen.


Figure 6 How R works

As can be seen from Figure 6, all operations performed in R are for objects stored in active memory. The input and output of data, results, or graphs is achieved by reading and writing files on the hard disk of the computer. The user can enter some commands to invoke the function, the results of the analysis will be displayed directly on the screen, can be stored in an object or written to the hard disk. Because the resulting result is an object in itself. So they can also be treated as data and analyzed as normal data. Data files can be read from a local disk or transmitted over a network from the remote server side.

All available R functions are included in a library, which is placed under the R_home/library folder of the disk (R_home is the address of the installation R, as the author's Library folder is: D:\Program files\r\r-3.2.3\ Library). This folder contains packages with various functions (packages), which are organized in the same way as folders.

In all packages, a package named base can be considered the core of R. Because it is embedded in the R language all like data read and write and operation of these most important functions.

In each package, there is a subfolder R, and this folder contains a file with the same name as the package (for example, in the package base, there is a file r_home/library/base/r/base), the file is the place to store all functions.

v. Objects of R
All operations performed in R are for objects stored in active memory. Therefore, the object occupies the core position in R.

1. Naming rules for objects
The name of the object must begin with a single letter (A-Z or a-Z), which can include letters, numbers (0-9), dots (.). and underline (_).
At the same time, the R object's name is distinguished from uppercase and lowercase, so like x and X can represent two completely different objects.

2. Simple manipulation of objects
(1) Assignment of objects
An object can be generated by an assignment operation. The assignment ("ssign") symbol in the R language is usually an arrow-shaped flag that consists of an angle bracket and a minus sign. The symbol can be left-to-right in the direction. can also be reversed.
Like what. We are able to open the execution interface of the R software. Assigns a value to the object n on the interface.

> n <- 15> n[1] 15> 5 -> n> n[1] 5

The number 1 in the square brackets in the execution result line indicates that the first element of n begins to display. We can see. Objects in R can be used directly, rather than in very many programming languages, and must be defined before they are used. Assuming that the object already exists, its former value will be flushed out of the new value itself (this is the same as most programming languages).

(2) Uppercase and lowercase objects
As previously mentioned, the name of the R object is distinguished by uppercase and lowercase, for example. We can assign different values to objects x and X.

> x <- 1> X <- 10> x[1] 1> X[1] 10

(3) Diversity of assignment methods
In R. Assigning a value to an object can be in many forms, either directly assigning a value or as a result of a calculation or a function.

> n <- 10 + 2> n[1] 12> n <- 3 + rnorm(1)> n[1] 3.819211> (10 + 2) * 5[1] 60

Execution Rnorm (1) will produce a random variable with a standard normal distribution that obeys the average of 0 and a standard deviation of 1.

We can also simply enter a function or an expression without assigning its result to an object, but the results shown in the form will not be saved in memory.

(4) Display in-memory objects
In R, use semicolons (;) to separate the different command statements in the same row. Variables of type string are enclosed in double-cited ("").
function ls is the function of displaying all objects in memory, but only the name of the object is listed.

"Carmen"101000.5> ls() [1"A"      "compar""m"      "M"      "n"      "N"      "n1"     "n2"     [9"name"   "x"      "X"      "z"   

Suppose you just need to show an object with a specified character in the name. This is done by setting the option pattern (abbreviated to PAT).

ls"m")[1"compar""m"      "name"  

Suppose further limited to objects that appear with a letter beginning with a name, you can:

ls"^m")[1"m"

The Execute function ls.str () will show the details of all objects in memory.

> ls.str () a:chr"Gomphotherium"Compar:logiTRUEM:Num 0.5M:' Data.frame ':1Obs. of  3Variables: $ n1:Num Ten$ N2:Num  -$ M:Num 0.5N:Num 3.82N:Num 2.1e+23N1:Num TenN2:Num  -Name:chr"Carmen"X:Num 1X:Num TenZ:cplx0+1I

(5) Deleting objects
To delete an object in memory. The function rm can be used. executing RM (x) will delete object x, and executing RM will delete objects X and Y, while RM (List=ls ()) will delete all objects in memory.


Of course, some of the options in the LS () function can be applied to the RM as well. To select certain objects, such as RM (List=ls (pat= "^m")) to be deleted.

3. Category Overview of Objects
7 of what you see.

Overview of the R language

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.