官术网_书友最值得收藏!

Vector indexing

Elements of a vector can be arranged in several haphazard ways, which can make it difficult to access them when needed. Hence, indexing makes it easier to access the elements.

You can have any type of index vectors, from logical, integer, and character.

Vector of integers starting from 1 can be used to specify elements in a vector, and it is also possible to use negative values.

Let's see some examples of indexing:

  • Returns the nth element of x:
x <- c(9,8,1,5)
  • Returns all x values except the nth element:
x[-3]
## [1] 9 8 5

  • Returns values between a and b:
x[1:2]
## [1] 9 8
  • Returns items that are greater than a and less than b:
x[x>0 & x<4]
## [1] 1

Moreover, you can even use a logical vector. In this case, either TRUE or FALSE will be returned if an element is present at that position:

x[c(TRUE, FALSE, FALSE, TRUE)]
## [1] 9 5
主站蜘蛛池模板: 嘉祥县| 河东区| 当雄县| 平南县| 安宁市| 临潭县| 唐河县| 乌拉特中旗| 巨野县| 镇赉县| 丹东市| 盐津县| 沂源县| 大兴区| 景德镇市| 板桥市| 望谟县| 广丰县| 姚安县| 留坝县| 高密市| 渝中区| 高要市| 津南区| 吴桥县| 体育| 五莲县| 玛沁县| 通化市| 宝丰县| 和静县| 桂东县| 尉氏县| 余姚市| 丹阳市| 安义县| 林口县| 莆田市| 长丰县| 银川市| 永仁县|