4.1 Vector
4.1.1 Sequence
In addition to using the colon operator, R supports other functions to create more general sequences. The SEQ function is the most common and can be used to specify sequences in many different ways. In the normal use of different scenarios, we have the following three types of functions to use, they are more efficient and easier to use, to meet the use of specific occasions.
Seq.int can create a sequence whose range is specified by two numbers, and can also be set in steps, notably although Seq.int ends with an int, he also supports creating sequences of floating-point numbers, such as
Seq.int (0.1,0.01,-0.01) [1] 0.10 0.09 0.08 0.07 0.06 0.05 0.04 0.03 0.02 0.01
Seq_len function: Create a sequence from 1 to the input value
Seq_along function: Create a sequence starting from 1, length is the length of the input vector
PP <-C ("Peter", "Piper", "Picked", "a", "Peck", "of", "pickled", "peppers") for (I In Seq_along (PP)) print (Pp[i]) [1] "Peter" [1] "Piper" [1] "Picked" [1] "a" [1] "Peck" [1] "of" [1] "pickled" [1] "peppers"
PS: When reading official documents, it is advisable to master the use of SEQ () if it is not particularly in pursuit of operational efficiency or if your sequence is particularly long.
4.1.2 Length
We can find this value through the length function. The defect value is also counted in length.
4.1.3 naming
One big feature of r vectors is the ability to name each element. In general, markup elements can make code more readable.
x <-1:4 names (x) <-C ("Apple", "bananas", "kiwi Fruit", "") x apple Bananas kiwi fruit 1 2 3
4.1.4 Index Vector
In general, we only have to access parts or individual elements in the vector. This is the so-called index, which he implements with square brackets [].
1. If you pass a positive number to a vector, it returns a vector element slice at this position. His first position was 1 (peer Python is 0)
2. If you pass a negative number to a vector, it returns a vector slice with all elements except the negative absolute position.
3. Use the which function to return a position where the logical vector is true, which is useful if you switch the logical index to an integer index
X <-(1:5) ^2which (x > 10) [1] 4 5
4. Which.min and Which.max are abbreviations for which (min (x)) and which (Max (x)) respectively
4.1.5 Vector Loops and repeats
The rep function is ideal for such tasks, which allows us to reuse elements to create vectors
Rep (1:5, 3) [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
4.2 Matrix and array
Arrays are multidimensional matrices, so in addition to rows and columns, the array also requires the dimension Dim parameter
4.2.1 Creating arrays and matrices
Arrays: Array ()
Matrices: Matrix ()
4.2.2 rows, columns, and dimensions
For matrices and arrays, the Dim function returns the integer value vector of its dimension
For matrices, the functions Nrow and Ncol will return the number of rows and columns respectively
4.2.3 row name, column name, and dimension name
With the output dimension function type, the function that displays the row name, column name, and dimension name is Rownames (), Colnames (), Dimnames ()
4.2.4 Indexed arrays
The index matrix is similar to the index vector. To display all the elements, simply empty the corresponding subscript
4.2.5 merging matrices
You can use the Cbind and Rbind functions to bind two matrices by row and column
Cbind (A_matrix,another_matrix) # #横向合并 ein zwei drei vier funf sechsone 1 5 9 2 18two 2 6 4 20three 3 7 6 22four 4 8 8 rbind (A_matrix, Another_matrix) # #纵向合并 ein zwei dreione 1 5 9two 2 6 10three 3 7 11four 4 8 12five 2 18six 4 20seven 6 22eight 8 24
4.2.6 Matrix Arithmetic
Arrays and matrices support standard arithmetic operators (+ 、-、 x,/), in addition to these matrices
Transpose
The T function can be used to transpose the Matrix T (A_matrix)
Inner multiplication
A_matrix %*% t (a_matrix) # #内乘
Outer multiplication
1:3%o% 4:6 #外乘, O for the meaning of outer
Inverse of matrix
Using the solve function can be very simple to achieve the inverse of the matrix
4. Vectors, matrices, and arrays