R language introduction: vector index

The content of this section is based on the previous assignment of the most basic vector of R language. After learning R, I feel very comfortable about the index of vector. Because this is much better than Python's index. The index starts from where the index starts and ends where it ends, instead of inputting 0 as Python does sometimes, it actually starts from 1 Sometimes, by 99, you actually index to 100, which makes it hard to figure out. After learning the simplicity of R, he was very cheerful.

1, Get the value of a value in a vector

First of all, our husband becomes a vector from 1 to 100:

> x <- c(1:100)
> x
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29
 [30]  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58
 [59]  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87
 [88]  88  89  90  91  92  93  94  95  96  97  98  99 100
> length(x)
[1] 100

We can see that the length of this vector is exactly 100, not many, not 99, not 1011, extremely comfortable. Then use the index to extract the eighth number, let's see if it is 8:

> x[8]
[1] 8

2, Get rid of a value and get the rest

x[-19]
 [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  20  21  22  23  24  25  26  27  28  29  30
[30]  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59
[59]  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88
[88]  89  90  91  92  93  94  95  96  97  98  99 100

Here we use a minus sign to get the rest of the values except 19.

3, Index with vectors

> x[c(4:78)]
 [1]  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
[40] 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
> x[c(1,45,67,68)]
[1]  1 45 67 68

4, Add Boolean value to the vector for true or false judgment and output

> y <- c(1,2,3,4,5,6)
> y
[1] 1 2 3 4 5 6
> y[c(T)]
[1] 1 2 3 4 5 6
> y[c(T,F)]
[1] 1 3 5

What we can see from this is that we first introduce a Y vector, the first time all the digital active commands are judged as true, then all the numbers are output. The second continuous cycle is true first and then false, so only output 1,3,5. Of course, it is also commonly used to judge the value directly, or even add logic words, as follows:

y[y>2]
[1] 3 4 5 6
y[y>2 & y<100]
[1] 3 4 5 6

In R, a "& minus sign is used instead of two, which is also a point that looks extremely comfortable.

5, Judge whether a value (component) in vector is in vector

First, construct the vector x:

> x <- c("one","two","three")
> x
[1] "one"   "two"   "three"

Then use the function to judge:

> "one" %in% x 
[1] TRUE

The result is T, indicating that the element "one" is in the element we construct.

6, Add a value to an existing vector

The simplest way is to add the last face of the vector as follows:

> a <-c(1:100)
> a
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29
 [30]  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58
 [59]  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87
 [88]  88  89  90  91  92  93  94  95  96  97  98  99 100
> a[101]=101
> a
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29
 [30]  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58
 [59]  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87
 [88]  88  89  90  91  92  93  94  95  96  97  98  99 100 101

Just add a number directly to the index of 101.

Then there is the method of adding a value after a certain value. For example, we need to add a value 99 after 3. Of course, we need to create a new vector V first. The code is as follows:

> v <- c(1:9)
> v[20] <- 20
> v
 [1]  1  2  3  4  5  6  7  8  9 NA NA NA NA NA NA NA NA NA NA 20
> append(x=v,values = 99,after=3)
 [1]  1  2  3 99  4  5  6  7  8  9 NA NA NA NA NA NA NA NA NA NA 20

This is the end of today's tutorial! I hope you can get something after seeing it!

Keywords: R Language REST Python

Added by ev5unleash on Tue, 25 Feb 2020 15:47:18 +0200