Vector Definition
In practical applications, a common object of R is vector ). The vector creation format is
Vector name = C (x1, x2, X3 ,.....)
OrVector name <-C (x1, x2, X3 ,.....)
OrC (x1, x2, X3 ,.....) -> Vector name
OrAssign ("vector name", C (x1, x2, X3 ,.....)),C () is a vector assignment function. C () can have any number of parameters, and the return value is a vector that connects these parameters at the beginning and end. Can you enter it in the command line? C or help (c) to view the function details. For example, to create a vector named x containing five values, you can use the following method:
X = C (7.1, 6.3, 5.8, 2.4, 12.7). The following figure is displayed in rstudio:
> X = C (7.1, 6.3, 5.8, 2.4, 12.7)
Press enter. To view the specific value of the X vector, you only need to input X in the command line. As shown in
> X
[1] 7.1 6.3 5.8 2.4 12.7
The preceding [1] indicates the first element of the obtained vector.
Vector operations
Take vector X as an example. If we input the 2 * x command, then every element in the vector is multiplied by 2 to get a new vector, we can also assign the new vector to a new object. At the same time, the value of each element in vector X does not change. The results of the preceding operations are as follows:
> X
[1] 7.1 6.3 5.8 2.4 12.7
> 2 * x
[1] 14.2 12.6 11.6 4.8 25.4
> Y = 2 * x
> Y
[1] 14.2 12.6 11.6 4.8 25.4
> X
[1] 7.1 6.3 5.8 2.4 12.7
The preceding values are relatively simple values. For more information, see the following example.
> If y = C (x, 0, x), What is the condition of the element of vector y?
Enter Y in the console to view the result
> Y
[1] 7.1 6.3 5.8 2.4 12.7 0.0 7.1 6.3 5.8 2.4 12.7
Note that r creates a vector y containing 11 elements, including two copies of X and a 0 in the middle.
In the above example, each element of our X vector is a numerical value. In fact, the vector can also accept elements of the string type. For example, you can input the following command to generate a vector of three string elements.
> STR = C ("H", "time", "vector ")
> Str
[1] "H" "time" "vector"
The following is the input result. If we want to perform 2 * STR operations on the STR vector, R will report an error, prompting us that STR is a vector composed of Non-numeric elements.
In addition to basic +,-, *,/, and power ^ operations, vectors also include some common mathematical functions, such as log, exp, sin, cos, tan, SQRT, and so on; max and min can calculate the maximum and minimum values of a vector. If the vector element is of the string type, then, the max and Min functions obtain the final and foremost values of the vector elements sorted in string order. The mean () and VAR () functions obtain the mean and variance of the vectors respectively, in the preceding example, vector y uses the preceding two functions to calculate the mean value and variance. The result is as follows:
> Mean (y)
[1] 6.236364
> VAR (y)
[1] 15.37655
Many functions can be used to obtain more information through viewing the help documentation.
Select a subset from a vector
If we want to access some elements of a vector, we can add [] After the vector name to select a subset of the vector.
> V = C (1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9)
> V
[1] 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
> V [2]
[1] 2.3
> V [2: 6]
[1] 2.3 3.4 4.5 5.6 6.7
> V [C (1, 2, 1)]
[1] 1.2 2.3 1.2
The above operations are to obtain the second element of the vector V, get the value of the second to sixth element of the vector V, and get the value of the first, second, and repeated first element of the vector v.