R Vectors – Create, Access, Modify and Delete a Vector Elements in R

Sharing is Caring

A Vector in R is basically a set of values of the same basic data type like numeric character etc. Vector in R is created using the c() function that represents a combination of elements.

I have posted basics about R Vectors in the previous post, Here we’ll learn more about Vector data type.

Creating a Vector in R

In R even a single value is considered as a vector of length 1, We can create a multi-element R Vector using a colon (:) like this

#Creating a vector using colon
v <- (1:10)
#Print its values
print(v)

and output is

[1]  1  2  3  4  5  6  7  8  9 10

Create a vector using c() function

# A Numeric Vector
numeric_vector <- c(10, 20, 30)
# A Character Vector
character_vector <- c("a", "b", "c")
# A Boolean Vector
boolean_vector <-c(TRUE,FALSE,TRUE)

now print these vectors

> #Print All Vectors
> print(numeric_vector)
[1] 10 20 30
> print(character_vector)
[1] "a" "b" "c"
> print(boolean_vector)
[1]  TRUE FALSE  TRUE

Accessing Vector Elements in R

Vectors in R works on the concept of the index and to access vector elements value we need to use the index, Indexing in R starts from 1 and here we’ll see that how to access a vector’s value

#Define a Vector
v <- c(10,20,30,40,50,60,70,80)

#Get element's value using index
a <- v[c(1,3,4,6)]

#Print values
print(a)

and output is

[1] 10 30 40 60

We can also use boolean value (TRUE, FALSE) to get elements of the vector, See an example here

#Define a Vector
v <- c(10,20,30,40,50,60,70,80)

#Get element's value using boolean value
a <- v[c(TRUE,FALSE,FALSE,FALSE,TRUE,TRUE,FALSE,TRUE)]

#Print values
print(a)

and output is

[1] 10 50 60 80

Using negative value as index removes that particular element from the result, see an example

#Define a Vector
v <- c(10,20,30,40,50,60,70,80)

#Get element's value using negative index
a <- v[c(-2,-4,-6,-8)]

#Print values
print(a)

and output is

[1] 10 30 50 70

R Vectors - Create, Access, Modify and Delete a Vector Elements in R

Arithmetic Operation in Vectors

We can perform variables like arithmetic operations (Addition, Subtraction, Multiplication, Divison) in vectors too.

#Add two vectors
a <- c(1, 2, 3)
b <- c(4, 5, 6)

# Addition
c <- a+b

# Subtraction
d <- a-b

# Multiplication
e <- a*b

# Divison
f <- a/b

now on printing this

> #Print Values
> cat("Addition",c)
Addition 5 7 9> 
> cat("Subtraction",d)
Subtraction -3 -3 -3> 
> cat("Multiplication",e)
Multiplication 4 10 18> 
> cat("Divison",f)
Divison 0.25 0.4 0.5>

Modifying a Vector in R

We can modify vector’s elements values using index and assignment operator. See this example

#Define a Vector
v <- c(10,20,30,40,50,60,70,80)

#Modify 3rd element value
v[3] <- 33;

#Print Updated Vector
print(v)

and output is

[1] 10 20 33 40 50 60 70 80

Deleting a Vector in R

We can delete a vector by setting a vector to NULL.

#Define a Vector
v <- c(10,20,30,40,50,60,70,80)

#Set its value to NULL
v <- NULL

#Print Vector
print(v)

Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

Leave a Reply

Your email address will not be published. Required fields are marked *