R Language Programming Art # data type vector (vector)

Source: Internet
Author: User

The most basic data type of R language-vectors (vector)

1, insert the vector element, all elements in the same vector must be the same pattern (data type), such as Integer, numeric (floating-point), character (string), logical, plural, and so on. View the type of the variable can be queried using the typeof (X) function.

> #插入向量元素 > x <-C (88,5,12,13) > X[1]  5 13> x <-C (x[1:3],168,x[4]) #插入168数字在13之前 > x[1]  8 8   5  12 168  

2, delete the elements in the vector, because the vector in R is continuously stored, it is not possible to insert or delete the element (so the above insert code actually re-creates a new vector and then points x to the new vector, similar to the pointer in C)

> #删除向量中的元素 ^_^ > X[1]   5  168  13> x <-C (88,5,168,13) > X[1]   5 168  

3, get the long vector

> #获取向量的长量 > X[1]   5 168  

4. Iterate through all the elements in the vector

> #第一种方法 because 1:length (x) = (1,0), actually made two iterations > first1function (x) {for (I in 1:length (x)) {    
> #第二种方法 generates a linear sequence with the SEQ function, with an element interval of 1, which solves the first inefficient method > First2 <-function (x) {+ for     (i in SEQ (x)) {+         if (x[i]==1) break+     }+     

5, vector and array, matrix array and matrix including the list, in a sense is actually a vector. It's just that they have extra class properties. such as: The Matrix has the number of rows and more.

> M <-Matrix (C (1,2,3,4), nrow = 2,byrow = T) > M     [, 1] [, 2][1,]    1    2[2,]    3    4> m + 10:13     [, 1] [, 2] [1,]   14[2,]   14   

Here the 2x2 matrix M is stored as a four-tuple vector, i.e. (1,2,3,4), and is stored as two lines, sorted by rows, and then added (10,11,12,13) to it, with the latest matrix, equivalent to the following code

> M <-Matrix (C (1,3,2,4), nrow = 2) > M     [, 1] [, 2][1,]    1    2[2,]    3    4> m + 10:13     [, 1] [, 2][ 1,]   14[2,]   14   

6, loop completion when using operators on two vectors, if the two vectors are required to have the same length, R automatically loops over (recycle), which repeats the shorter vector until it matches the length of another vector

> C (1,2,4) + C (6,0,9,20,22) [1]  7  2 24Warning message:in C (1, 2, 4) + C (6, 0, 9, 20, 22):  

Equivalent to the following code:

> C (1,2,4,1,2) + C (6,0,9,20,22) [1]  7  

#矩阵

> x <-Matrix (C (1,2,3,4,5,6), nrow = 3) > x     [, 1] [, 2][1,]    1    4[2,]    2    5[3,]    3    6> x + C (up to)     [, 1] [, 2] [1,]    2    6[2,]    4    6[3,]    4    

#矩阵循环补齐

> x <-Matrix (C (1,2,3,4,5,6), nrow = 3) > x     [, 1] [, 2][1,]    1    4[2,]    2    5[3,]    3    6> y <-Matrix (C (1,2,1,2,1,2), nrow = 3) > y     [, 1] [, 2][1,]    1    2[2,]    2    1[3,]    1    2> x + Y     [, 1] [, 2][1,]    2    6[2,]    4    6[3,]    4    

Equivalent:

    

7. Common vector operations include arithmetic and logic operations, vector indexing, creating vectors, etc.

#R是一种函数式语言, it's every shipping symbol (+-*/...) ) are actually all functions

#加法 > 2+3[1] 5> "+" (2,3) [1] 5>>c (+) + C (3,4) >[1] 4  6>> "+" (2,3,4) Error in ' + ' (2, 3, 4): O Perator needs one or both arguments> #乘法 > C (+) * C (3,4) [1] 3 8> #减法 > C (3,4)-C (+) [1] 2 2> #除法 > C ( 3,4)/C (on) [1] 3 2> #取余 > C (3,4) percent C (2,3) [1] 1 1>

8. The most important and most common operator in the vector index R is the index, which is used to select the elements of a specific index in the directed amount to form a sub-vector. The format of the index vector is x[y] (x, y are vectors), which returns the result of those elements in X that are indexed as Y.

R Language Programming Art # data type vector (vector)

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.