R language: vector and assignment

Source: Internet
Author: User

From: http://bigdata.iteye.com/blog/1777022

Vector and assignment

R works on the named data structure. The simplest structure is a numerical vector consisting of a string of ordered values.

X <-C (1, 2, 3, 4, 5, 6)

This is a value assignment statement completed by Function C. (<-Yes)

Note: equivalent to assign ("X", C ))

Vector operations

The basic arithmetic operators are commonly used +,-, *,/and ^ for power operations. It also includes common mathematical functions, such as log, exp, sin, cos, tan, and SQRT.

Min (X) and max (x ):Returns the functions of the minimum and maximum values of vector X;

Range (x) function:Is a vector with a length of 2, that is, C (min (x), max (x ));

Length (x) function:Number of elements in vector X;

Sum (x) function:Calculates the sum of elements in vector X;

Prod (x) function:Obtains the product of elements in the X vector;

Mean (x) function:Calculate the mean value, which is equivalent to sum (X)/length (X );

VaR (x) function:Calculate the sample variance, which is equivalent to sum (X-maen (z) ^ 2)/(length (x)-1)
(If the VaR () parameter is an N * P matrix, the rows and rows of the matrix are considered as sample vectors of the P-variables, so as to obtain a sample covariance matrix of p * P );

Sort (x) functions:Returns a vector with the same length as X but with elements in ascending order. (Order () and sort. List () sorting functions are more powerful)

Generate Regular Sequence

R has some tools for generating commonly used columns.

For example, is equivalent to vector C ,......, 29,30 );

Note: In the R expression, the colon priority is the highest.

Function seq ()Is the most commonly used tool in sequence generation. It has five parameters, only some parameters need to be

Are set. The start and end parameters of a series. If only the two values are given

The effects of the operators are the same. For example, seq () is equivalent.

SEQ () is the same as that of many other R functions. In this case,

The order of parameters can be arbitrary. In this way, the first two parameters can be in the from = value and to = value modes.

Set; therefore, seq (), seq (from = 1, to = 30), seq (to = 30, from = 1) is exactly the same

Sample. SEQ () the following two parameters are by = value and length = value; they represent the steps of this series respectively.

Length and length. If the two are not set, the default value is by = 1 (step size is 1 ).

For example, seq (-5, 5, by =. 2)-> S3 is equivalent to vector C (-5.0,-4.8,-4.6 ,......, 4.6, 4.8, 5.0) is assigned to S3; similarly, S4 <-seq (length = 51, from =-5, by =. 2), the same vector will be generated in S4.

The fifth parameter is along = vector. When this parameter is used, it must be unique 4 and can be created

Column 1, 2,..., length (vector), or an empty series (when the vector is empty ).

Rep () function: You can repeat an object in a variety of complex ways. The simplest way is S5 <-Rep (x, Times = 5). In this way, we first copy x five times to keep the sequence of X in S5. Another useful

The method is S6 <-Rep (x, each = 5). In this way, each element in X is repeated five times, and the elements repeated five times are put one by one.

Logical Vector

Logical vector elements can be assigned with true, false, and Na ("cannot be obtained "). The first two values can be abbreviated to T and F respectively.

Note:T and F are only equivalent variables set to true and false by default, not system reserved words (Reserved

And can be rewritten by the user. Because of this, you should try to use true and false which belong to the reserved words of the system.

The logical operator of R is<, <=,>, >=, And determine whether the conditions are strictly equal! =. In addition, if c1 and c2 are logical inequalities, C1 & c2 are their intersection operations ("and"), C1 | C2 is union operation ("or "),! C1 is a non-computation of C1.

Note:When logical variables are used in regular arithmetic operations, they are forcibly converted to numerical variables, false to 0, and true to 1.

Defect Value

In some cases, vector elements may be incomplete.

When an element or value is counted as "not available" or "Missing Value", the relevant position may be retained and a specific value Na is assigned. Any calculation result containing NA data will be na.

The function is. Na (x) returns a vector of the same length as X. Its element value is true only when the corresponding element in X is Na.

The value calculation produces the second "defect" value, also known as a non-value (not a number) Nan.

Character Vector

The paste () function can have any number of parameters and concatenate them into strings.

For example, labs <-paste (C ("X", "Y"), SEP = "")

Converts labs into a character vector.

C ("X1", "Y2", "X3", "Y4", "X5", "y6", "X7", "Y8", "x9 ", "Y10 ")

Index Vector

Logical vector:In this case, the Index Vector must be the same as the vector length of the selected element. Elements with the corresponding Index Vector element true in the vector will be selected, while those with the corresponding false element will be ignored, for example, Y <-X [! Is. Na (x)]. This creates (or re-creates) an object y that contains non-default and unchanged elements in X. NOTE: If X contains the default value
The length will be shorter than X.

(X + 1 )[(! Is. Na (x) & x> 0]-> Z

Create an Object Z and assign the value of the vector x + 1 to it, which requires that the elements in X are both non-default and positive.

Positive Integer vector:For example, X [6] indicates the sixth element of X;

X [], select the first 10 elements of X (we suppose the length (x) length is not less than 10 );

C ("X", "Y") [rep (C (,), Times = 4)], produces a length of 16, from "X ", "Y", "Y", "x" repeats the vectors four times.

Negative vector:This index vector specifies excluded elements rather than included. Therefore, Y <-X [1 ()] assigns all the elements except the first five to y.

String vector:This may only be used for an object to identify its elements using the name attribute. In this case, the subvector of the name vector can be used like the positive integer label mentioned in the second article above.

> Fruit <-C (5, 10, 1, 20)

> Names (fruit) <-C ("orange", "banana", "apple", "Peach ")

> Lunch <-fruit [C ("apple", "orange")]

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.