- Machine Learning with R Quick Start Guide
- Iván Pastor Sanz
- 122字
- 2021-06-24 16:01:29
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
推薦閱讀
- 大數據戰爭:人工智能時代不能不說的事
- 傳感器技術實驗教程
- Learning Social Media Analytics with R
- Python Data Science Essentials
- Windows內核原理與實現
- 大數據平臺異常檢測分析系統的若干關鍵技術研究
- STM32嵌入式微控制器快速上手
- 基于32位ColdFire構建嵌入式系統
- ESP8266 Home Automation Projects
- JavaScript典型應用與最佳實踐
- Learn CloudFormation
- RedHat Linux用戶基礎
- 走近大數據
- Mastering Geospatial Analysis with Python
- 手機游戲策劃設計