R Vectors

A Vector in the R programming language are items of the same datatype. In this lesson, we will learn how to create a Vector and work on some Vector operations:

  • Create a Vector
  • Access Vector Items
  • Vector Length
  • Sort Vector Items
  • Add Vectors
  • Subtract Vectors
  • Multiply Vectors

Create a Vector

The c() method is used to create a Vector in R. The Vector items are separated by comma. Let us see the examples to create a Vector with:

  • Numerical Values
  • Numerical Values in Sequence
  • String values
  • Boolean values

Let us create a Vector, beginning with numerical values.

Create a Vector with numerical values

Use the c() function to create a vector of numerical values in the R programming language:

# Vector of numerical values
myVector <- c(10, 20, 30, 40, 50)

# Display the Vector
myVector

Output

[1] 10 20 30 40 50

Create a Vector with numerical values in a sequence

The : operator is used in R to create a vector with numerical values in sequence:

# Vector of numerical values in a sequence
# The : operator is used below
myVector <- 40:50

# Display the Vector
myVector

Output

[1] 40 41 42 43 44 45 46 47 48 49 50

Create a Vector with string values

We will use the c() function to create a vector with string values:

# Vector of string values
myVector <- c("amit", "rohit", "virat", "tom", "john")

# Display the Vector
myVector

Output

[1] "amit"  "rohit" "virat" "tom"   "john"

Create a Vector with boolean values

We will use the c() function to create a vector with boolean values. The boolean includes only TRUE and FALSE values:

# Vector of boolean values
myVector <- c(FALSE, TRUE, FALSE, FALSE, TRUE)

# Display the Vector
myVector

Output

[1] FALSE  TRUE FALSE FALSE  TRUE

Access Vector Items

Use the [] brackets to access vector items i.e., elements of an R vector. Just set the position number of the element you want to fetch. Follow the below syntax:

# Vector of string values
myVector <- c("amit", "rohit", "virat", "tom", "john")

# Display the Vector
myVector 

# Access Vector items
# Set the position of the item you want to access inside the [] brackets
# Access the 3rd item
myVector[3]

Output

[1] "amit"  "rohit" "virat" "tom"   "john" 
[1] "virat"

In the above example, we have accessed the 3rd item since we have set position 3.

Vector Length

The length() function is used in the R programming language to get the vector length. Let us see an example:

# Vector of string values
myVector <- c("amit", "rohit", "virat", "tom", "john")

# Display the Vector
myVector 

# Get the length of the Vector
length(myVector)

Output

[1] "amit"  "rohit" "virat" "tom"   "john" 
[1] 5

Sort Vector Items

The sort() function is used in the R programming language to sort the vector items. Let us see an example:

# Vector of string values
myVector <- c( "rohit", "virat", "amit", "tom", "john")

# Display the Vector
myVector 

# Sort the vector items
sort(myVector)

Output

[1] "rohit" "virat" "amit"  "tom"   "john" 
[1] "amit"  "john"  "rohit" "tom"   "virat"

Add Vectors

In the below example, we will add two vectors in the R programming language:

# Vector1
myVector1 <- c(10, 20, 30, 40, 50)

# Vector2
myVector2 <- c(5, 15, 25, 35, 45)

# Display the Vectors
print("Vector1:")
myVector1
print("Vector2:")
myVector2

# Add Vectors
print("Sum of Vectors:")
result <- myVector1 + myVector2

# Display the result after adding two vectors
Result

Output

[1] "Vector1:"
[1] 10 20 30 40 50
[1] "Vector2:"
[1]  5 15 25 35 45
[1] "Sum of Vectors:"
[1] 15 35 55 75 95

Subtract Vectors

In the below example, we will subtract two vectors in the R programming language:

# Vector1
myVector1 <- c(10, 20, 30, 40, 50)

# Vector2
myVector2 <- c(5, 15, 25, 35, 45)

# Display the Vectors
print("Vector1:")
myVector1
print("Vector2:")
myVector2

# Subtract Vectors
print("Result of Subtracting Vectors:")
result <- myVector1 - myVector2

# Display the result after subtracting two vectors
result

Output

[1] "Vector1:"
[1] 10 20 30 40 50
[1] "Vector2:"
[1]  5 15 25 35 45
[1] "Result of Subtracting Vectors:"
[1] 5 5 5 5 5

Multiply Vectors

In the below example, we will multiply vectors in the R programming language:

# Vector1
myVector1 <- c(10, 20, 30, 40, 50)

# Vector2
myVector2 <- c(5, 15, 25, 35, 45)

# Display the Vectors
print("Vector1:")
myVector1
print("Vector2:")
myVector2

# Multiply Vectors
print("Result of multiplying Vectors:")
result <- myVector1 * myVector2

# Display the result after multiplying two vectors
Result

Output

[1] "Vector1:"
[1] 10 20 30 40 50
[1] "Vector2:"
[1]  5 15 25 35 45
[1] "Result of multiplying Vectors:"
[1]   50  300  750 1400 2250

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

R Arrays
R Lists
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment