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

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
主站蜘蛛池模板: 武陟县| 安丘市| 卓资县| 延吉市| 定襄县| 麦盖提县| 和硕县| 宁国市| 金昌市| 蒲江县| 中西区| 石台县| 彰化市| 扶沟县| 修水县| 冀州市| 新源县| 邯郸市| 潮安县| 宁陕县| 梁山县| 余庆县| 常熟市| 图片| 安溪县| 新余市| 桐柏县| 合山市| 昆明市| 定结县| 忻州市| 石阡县| 四平市| 桃江县| 通海县| 永州市| 唐山市| 宽甸| 淮滨县| 特克斯县| 新疆|