R Language Programming _r language

Source: Internet
Author: User
Tags mixed scalar
1.R Language Programming features 1.1 R language data structure

The R language data structure appears in the form of data types, with data boxes, arrays, vectors and matrices, factors, and lists. Data frame dataframe is a common data set in statistical specialty, and factor factor is the method used in classification, which indicates the distinctive economic statistic color of R language. Vector vectors are similar to one-dimensional arrays, but without row and column names, only the label names. Array arrays include one-dimensional arrays, two-dimensional arrays and three-dimensional arrays and multidimensional arrays, with rows, columns, and layers in three-dimensional arrays, with row names rownames and column names colnames. Matrix matrices are two-dimensional arrays, but have the properties of matrix computations. List lists not only have the features of C language structure struct, but also the nature of vectors, so it is a generalized table in the data structure.

In the data structure of the R language, there are no trees or graphs. The tree can be implemented using a static array, and the indexing method is applied. and the graph in the package Igraph provides the function realization, R language's graph is called the network data format, therefore can analyze the biological structure and the computer network. 1.2 R language is a high-level language

The R language has a program control structure and function functions. Although the R language is an interpreted language, it can be implemented in a compiled way, which is called source file. R) command. The R language package can be installed in a programming environment by downloading the submenu, so it is accessible. There are now more than 7,000 software packages in statistics, mathematical calculations, financial data analysis, biology, Internet data analysis, large data, parallel computing, mixed programming, data mining, data analysis and practical applications.

The command to load the package in the program,

>library (parallel) #并行计算软件包, single-threaded multicore

Find all functions in a package,

>library (Help=parallel)

>help (Packet=parallel)

R language is the same as an interpreted language, and there is no declaration of variables. R language can write C + + program, apply Rcpp software package. The R language does not have an independent scalar, only a circular statement using a scalar as a cyclic variable. See the Industry Encyclopedia of the R Language encyclopedia [4]. 1.3 R language Programming style

The R language of statistics, known as data manipulation, is simply processing data by command, grouping data, estimating parameters and testing results. Financial data analysis, is to establish a model, test model data fitting effect, so the data operation seems to have no obvious programming significance, in fact, there is no obvious procedural style.

However, there are standard processes for establishing models for financial data analysis, such as garch models with a minimum of 4 stages or steps, data properties, arch effects, establishment of garch models, and model-fitting optimization and model coefficients and parameter adjustments. Therefore the statistical analysis still has the procedure standard process, therefore can demonstrate programmer's programming ability and the unique style.

Computer Professional R language, known as the art of programming. The application method of factor, mixed programming, parallel programming, writing software package, all need superb programming technology and distinct programming style, so the function of different software package runs different time.

The function of the R language Program execution time is system.time (function), and the parameters are functions performed by the system. 2. R Language Program Design 2.1 R Language Programming method

R language deals with three kinds of problems, 1. Data manipulation. Including statistical analysis, such as the number of people who smoke in adverse health surveys, grouped data, etc. 2. Financial data analysis requires the establishment of an economic statistical model, such as the establishment of an Arima model; 3. Problem handling, such as data mining, high-level language programming, parallel computing, etc. There are three different programming methods for the problem. The key to data manipulation is to select the next command based on the results of each command, which requires rich practical experience and highly qualified problem-control capabilities. The output command result method has the plot diagram, the box chart and so on, the higher level is the application high quality report. High-quality report direct viewing and shopping malls POS machine with a similar, can be based on accurate data selection. The effect of establishing an economic statistical model is determined by the professional competence of the programmer. Sometimes, the model data fitting effect is quite reasonable, but can not carry on the detailed specialized explanation and the question analysis, therefore cannot further enhance the model the superiority, causes the computer to assist the data analysis the function to be weakened. The standard process of establishing a model is analyzed in teaching materials and reference books, but the mastery of the details indicates that the staff are careful in different degrees, and the effectiveness and accuracy of the procedures are different.  The third problem is that the application of R language by computer professionals not only requires the programming ability of computer software and the application of theory, but also has the standard of R language [1][2][3][5]. 2.2 Computer Majors master the standard of R language

(1) can use R language program to deal with all the problems of the data box, to achieve packet data and statistics, to achieve data structure programming, such as trees, graphs and so on.

(2) can use r language to achieve parallel computing, Windows system software package Parallel,rmpi,snow, multi-core and multiple computer cluster system.

(3) can realize the mixed programming with R language, can realize multi-core multithreading. For example: C/c++,java,python,fortran.

(4) can process a large amount of data in the R language. Ability to write high-performance programs to handle tens of thousands of of data, to achieve large database Oracle and Windows SQL processing data.

(5) can use R language to achieve the graphical interface. R language is a process language, but it is difficult to implement the graphical interface of the program. The shiny package enables HTML applications.

(6) can use R language to generate High-quality reports. Visually look at the R language report similar to the POS machine.

(7) can use R language program to realize data mining technology, such as K-means, association rules. can use the R language to achieve Hadoop, to achieve large data processing.

(8) can use R language to achieve data analysis, such as: financial data analysis, biology, accounting and finance, business and sales, telecommunications and communications, transportation, medical, new drug research and development and transport logistics. 2.3 R Language Parallel computing

Parallel computing has a practical application of multi-core multithreading, such as Python, Java, C++,mpi and openmp,r language packages are parallel,rmpi,snow, and GPU computing Cuda,r language uses gputools packages. R-language parallel computing enables single-threaded multi-core and cluster approaches, but the problem is that multiple-core multithreading is not possible, so call the R language program in the Invoke Python, Java, and C + + programming environments. Mixed programming program execution time is a matter of concern, C + + handler execution time function see below.

1.Linux system

Before the program PROGRAM1 executes, use parameter time on the command line./progarm1. The program implementation method is:

#include <sys/time.h> #时间处理头文件

#include <stdio.h>

#include <math.h> #数学头文件

void Functionsample () {#需要执行的函数

unsigned int i,j;

Double y;

for (i=0;i<1000;i++)

for (j=0;j<1000;j++)

Y=sin ((double) i); #计算正弦函数1000000次

}

Main () {

struct Timeval tpstart,tpend;

float Timeuse;

Gettimeofday (&tpstart,null); #tpstart Start time

Functionsample (); #执行函数functionsample

Gettimeofday (&tpend,null); #tpend End Time

timeuse=1000000* (tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec;

timeuse/=1000000;

printf ("Used time:%f\n", timeuse); #float型变量

Exit (0);

}

Output Result: Used time:0.556070 (refer to Internet article)

2.windows system

#include <time.h>

application function clock (), type clock_t, returns the number of milliseconds that the program execution begins to the time period between which this function is called. The clock_t is a long integer, and the constant clocks_per_sec indicates how many clock ticks are in a second, and the constants are defined below.

#define CLOCKS_PER_SEC ((clock_t) 1000)

Example1. To calculate the execution time of a circular statement

int main () {

clock_t StartTime, Endtime; #定义开始和结束时间

StartTime = Clock ();

for (int i = 0; i < 1000000 i++) #需要计算执行时间的循环时间

{i++;

}

Endtime=clock ();

cout << "Totle Time:" << (Double) (endtime-starttime)/clocks_per_sec << "s" << Endl;

System ("pause");

return 0;

Advanced application of 2.4 R language programming

(1) R language to write high-quality reports

The table is the basis for generating statistical reports in the R language. We see the medical test sheet, the form of image reports are medical professional, concise, mathematical expression, R language statistics and computational characteristics similar to this. Making high quality reports is called "Show your Best side". In the R language programming environment or the Rstudio graphics environment, the R markdown and KNITR packages can create data analysis reports. R Mardown provides a simple syntax for generating analysis reports, including plain text, r code, and inline code. KNITR builds HTML, PDF, Word document Format reports, and slides, and generates the execution results of the R code at the specified location. Package Knitr and Rmarkdown.

Library (KNITR)

Library (Rmarkdown)

To be Continued

Reprint Address: Http://www.baike.com/wiki/R%E8%AF%AD%E8%A8%80%E7%BC%96%E7%A8%8B

"The poetry and the song Fu"

                                                                                                            Jiangcheng son

Su shi

Ten years of life and death two boundless.
Don't contemplate, since unforgettable.
Trinidad solitary grave, nowhere to talk desolate. [1]
Even if we meet,
The dust is on the face and the temples are frost.

Night to dream and return to the native.
Small Porch window, is dressing.
Care without words, only tears thousand lines.
It's going to be heartbroken every year,
Moon Night, short Matsuoka.

Note [1] su Shi's memorial lady

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.