R introductory language: vector index

This section of the content is based on the R language before the foundation of our basic vector assignment above, completion of R author himself among the vector index paresthesia comfortable, because this index than Python's cool, what is the value of starting index index on where to start, where to end it where it ends, but not like Python sometimes enter 0 actually start counting from 1, sometimes up to 99 in fact you want to index to 100, giving a feeling kind of hard to figure out. Learn simple R cheerful heart greatly.

A. To give a numerical value of a vector which

First, we have Mr. into a vector of values ​​from 1-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

The length of the vector can see exactly 100 more nor less, than 99 nor 1011, extremely comfortable. Then use the index to extract the eighth number of them, let's see if it is 8:

> x[8]
[1] 8

II. Addition to a value other remaining values ​​obtained

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 represented by a negative number in addition to the 19, and the remaining values ​​obtained other represented.

III. Using the vector index

> 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

IV. Add vector which Boolean values ​​true and false judgment and outputs

> 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

We can see that the first introduction of a vector Y, the first all digital active command judgment is true, then all digital outputs. The second continuous cycle after the first true or false, so only outputs 1,3,5. Of course, it is more commonly used for directly determining the value, and even adding a logic word as follows:

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

In which R, using a "& minus sign instead of two, this is a point looks extremely comfortable.

Whether a value V. Analyzing vector (vector) among (Component) in which the vector

First constructed first vector x:

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

It is then determined using the function:

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

As a result T, explain "one" This element is in this we constructed elements among.

VI. The value added to the one that already exists among vector

The simplest is to add the rearmost vectors, 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

Digital can also be added directly in the index at 101.

Then after adding a numerical value, such a value we want to add 99 after 3, of course, we create a new vector V, code 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

Today's tutorial This is the end! I hope you can gain something after seeing!

Guess you like

Origin www.cnblogs.com/geeksongs/p/12363667.html