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

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
主站蜘蛛池模板: 三江| 保康县| 饶平县| 四子王旗| 汤阴县| 阳江市| 宜都市| 苏州市| 芮城县| 永年县| 呼图壁县| 宜良县| 九江市| 霸州市| 惠州市| 湖南省| 彩票| 房山区| 长岛县| 克什克腾旗| 固原市| 额尔古纳市| 济阳县| 呈贡县| 化隆| 胶州市| 梅州市| 黔江区| 延津县| 应城市| 亚东县| 哈密市| 砚山县| 宝丰县| 惠东县| 格尔木市| 襄汾县| 平阴县| 南昌县| 平利县| 镇平县|