Search This Blog

Tutorial # 3. Vector & Element Access

 What Is Vector?

A Vectors is a collection of similar types of object or Data Type. In most of the programming language indexing start from 0 but in R it start from 1.

'c' has important role, it check for the data type as they have priority sequence
Logical > Integer > Double > Characters
 
x = c(5,9,6,7,2,3,4,5)
print(x)

# Access a particular element
print(x[1])

# Access a range of elements
print(x[1:4])

# Access a range of elements with EXCLUDING the elements
print(x[c(-1:-4)])

# Accessing the elements with boolean values
print(x[c(FALSE , TRUE , FALSE , TRUE , FALSE , TRUE ,FALSE , TRUE)])
 
 
Example 1:   
 
# Creates a Vector 
vehicles = c('Car', 'Bike','Bus', 'Truck')
 
# Prints a vector 
print(vehicles)

# Creates a Vector 
numb = c(TRUE,1.6,2.2,5)
 
# Prints a vector 
print(numb)


Example 2: Creating a consecutive vectors "Starting Number : Ending Number"

x = 1:10
print(x)

y = 5:-5
print(y)

Example 3: Creating a vectors using " seq()" function

Syntax : seq( from , to , by , length.out )
from -  beginning of sequence
to - ending of sequence
by - increment by ( default is 1)
length.out -  divide the length of the sequence in equal part
 
x = seq (5,10)
y = seq (from = 0, to =10 , by = 2 )
z = seq (from = 0, to =10 , length.out =7)
print((x))
print(y)
print(z)

 

You may also interested in:

An Introduction to language R
Tutorial # 1.Data Type & Variable  Declaration
Tutorial # 2.Operators
Tutorial # 3.Vector  & Element Access
Tutorial # 4.Matrix
Tutorial # 5.Data Frame
Tutorial # 6.Arrays
Tutorial # 7.Lists
Tutorial # 8.Factors
Tutorial # 9.Data import
Tutorial # 10.Machine Learning
Tutorial # 11.Visualization


Post a Comment

0 Comments