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

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
主站蜘蛛池模板: 石阡县| 绩溪县| 化德县| 崇信县| 宜都市| 正安县| 庄浪县| 玛多县| 阿克陶县| 鄂州市| 筠连县| 新河县| 朝阳区| 沙湾县| 阿图什市| 南郑县| 大荔县| 始兴县| 九江市| 象山县| 大宁县| 民勤县| 凭祥市| 舒城县| 洛浦县| 韶山市| 秀山| 浦县| 浏阳市| 崇礼县| 咸阳市| 富锦市| 贡觉县| 建湖县| 黔西县| 安平县| 林州市| 滦南县| 高州市| 洛浦县| 深水埗区|