Page 224 - Data Science class 11
P. 224
b. using logical indexing ( starts with -1,-2 from left hand side)
Indexing is used in order to access the elements in a vector. The [] brackets are used for indexing. Indexing starts with
position 1. Providing a negative value in the index drops the element from the result. You can also use TRUE/FALSE
or 0 and 1 for indexing.
Example
Enter the following code snippet:
#Accessing vector elements using logical indexing.
t <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul")
v<- t[c(TRUE,FALSE,TRUE, FALSE, FALSE, TRUE, TRUE)]
print(v)
The following result is produced:
c. using negative indexing indexing (starts with -1,-2 from left hand side)
Example
Enter the following code snippet:
#Accessing vector elements using negative indexing.
z <- c("first", "second", "third", "fourth", "fifth", "sixth", "seventh")
x <- z[c(-1,-4)]
print(x)
The following result is produced (here it drops the elements at the index -1 and -4):
d. using 0/1 indexing
Example
Enter the following code snippet:
#Accessing vector elements using 0/1 indexing.
z <- c("first", "second", "third", "fourth", "fifth", "sixth", "seventh")
y<- z[c(1,0,0,1,0,0,1)]
print(y)
222 Touchpad Data Science-XI

