<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">
Go to top icon

Language R: Vectors

Abhay Wagh Nov 23, 2017

language R Vectors

Language-R-vectors.jpg

In my last blog Beginner's guide to R, I have written about the data types, variable, arithmetic and logical operations and introduction to data objects such as vector, matrix and data frames. In this blog I will discuss about vectors in details.
Vector is basic data object in R. It is one dimensional array which can hold data of same type of elements. There are six different type of Vectors.

 

  1. Logical
  2. Integer
  3. Double
  4. Character
  5. Complex
  6. Raw

Vectors can be created by using c() function having one or more elements separated by comma.

# Create Logical vector
c(TRUE, FALSE, TRUE)
# Create numeric vector
c(77, 80, 69)
# Create Double vector
c(77.5, 80.5, 69.5)
# Create Character vector
c("John", "Ravi", "Samual")
# Create complex vector
C(2+2i, 3+3i, 4+4i)
# Create Raw vector
charToRaw('hello')

Numerical vectors can also be created using colon or seq operator

# Create numeric vector using colon
numColonVector <- 1:10
print(numColonVector)
# Create numeric vector using seq
numSeqVector <- seq(1,10, by = 2)
print(numSeqVector)
If you execute these you will get output as
[1] 1 2 3 4 5 6 7 8 9 10
[1] 1 3 5 7 9

Naming a Vector

It is very essentials to have clear view of data. In vector c(77,80,69,81) we should know what these values are representing, are they simple numbers, marks of students, etc. Let’s say they are marks of students but it is hard to say 80 are marks in English or Ravi gets 80 marks in English. So to get clear view of data we can give name to element of vector using names() function.

Let’s say 77, 80, 69 are marks obtained by "John”, Ravi" and "Samual" in English

# Create vector with marks in English
MarksInEnglish <- c(77, 80, 69)
# Create vector for Student’s name
Students <- c("John", "Ravi", "Samual")
# Assign names to vector elements
names(MarksInEnglish) <- Students
# We can also write above command without creating student vector
names(MarksInEnglish) <- c("John", "Ravi", "Samual")
# Print vector
MarksInEnglish

Above code will give result as

John Ravi Samual
77 80 69

Vector selection

We can select any specific element or slice of vector using vector index.
# Create vector
MarksInEnglish <- c(77, 80, 69, 80, 59, 40, 100)
Students <- c("John", "Ravi", "Samual", "Nick", "Das", "Jack", "Sam")
names(MarksInEnglish) <- Students
# Retrieve value by name of element
MarksInEnglish ["Samual"]
Result will be 69
# Retrieve value by index of element
MarksInEnglish [3]
Result will be 69

Where 3 is index value for vector MarksInEnglish.

Negative index

We can assign negative index to exclude value of vector.
# Exclude second value from vector
MarksInEnglish [-2]
Result will be 77, 69,80,59,40,100
-2 index will excludes Ravi’s marks from output.

Out-of-Range

If the index is out of range it will give you value as “NA”
# Get 10th element of vector
MarksInEnglish [10]
Result will be NA

Index as Logical TRUE/FALSE

In place of index logical values can be used to get the elements
MarksInEnglish[c( TRUE ,TRUE ,FALSE ,TRUE ,FALSE ,FALSE ,TRUE)]
Result will be
John Ravi Nick Sam
77 80 80 100

Slicing of vector

Slicing can be done by specifying the index’s or names or by specifying the range of index.

If we want to retrieve slice of vector with first, third and fourth element

MarksInEnglish[c(1,3,4)]
Result will be
John Samual Nick
77 69 80

Slicing using names of vector
MarksInEnglish[c("Jack","Ravi")]
Result will be
Jack Ravi
40 80
MarksInEnglish[c("Ravi","Nick")]
Result will be
Ravi Nick
80 80

Slicing vector by assigning range of indexes. In below example vector range for 1 to 4 is specified.
MarksInEnglish[c(1:4)]
Result will be
John Ravi Samual Nick
77 80 69 80

Combining vectors

We can combine two vectors using c() function.
# create vector with marks in English
MarksInEnglish <- c(77, 80, 69)
# create vector with grade in Painting
GradeInPenting <- c("A","B+","A+")
# create vector for Student’s name
Students <- c("John", "Ravi", "Samual")
# assign names to vector elements
names(MarksInEnglish) <- Students
names(GradeInPenting) <- Students
c(GradeInPenting,marksInEnglish)
Result will be
John Ravi Samual john Ravi Samual
"A" "B+" "A+" "77" "80" "69"

In above example c(GradeInPenting,marksInEnglish) combine two vectors into one vector.

One important point in combining vector is you can see that all elements are converted in character string so as to maintain same primitive data types for each element.

Arithmetic operations

We can do different arithmetic operations on vectors. These operations are performed member by member.

# create vector with marks in English
MarksInEnglish <- c(77, 80, 69)
# create vector with marks in Science
MarksInScience <- c(90, 87, 62)
# create vector for Student’s name
Students <- c("John", "Ravi", "Samual")
# assign names to vector elements
names(MarksInEnglish) <- Students
names(MarksInScience) <- Students
MarksInEnglish +MarksInScience
Result will be
John Ravi Samual
167 167 131

In above example we are adding marks in English and Science of each students. 77 +90 = 167 are the marks obtained by John, 80 + 87 = 167 marks obtained by Ravi and 69 + 62 =131 marks obtained by Samual.

If the length of vector is not equal then shortest vector will be reused to match the longer vector and is called Recycling.

# vector with 2 elements
aVector <- c(10,20)
# vector with 5 elements
bVector <- c(100,200,300,400,500)
aVector+bVector
Result will be
110 220 310 420 510

Warning message:

In aVector + bVector
longer object length is not a multiple of shorter object length

Here 10+100 =110, 20+200 =220, 10+300 = 310, 20+400 =420, 10+500=510. If shorter vector is not in multiple of longer one we get warning message.

bVector <- c(100,200,300,400)
aVector+bVector
Result will be
110 220 310 420

We can do arithmetic operations between vector and numeric value. In below example numeric value 10 gets added to each element of vector.
bVector <- c(100,200,300,400,500)
bVector +10
Result will be
110 210 310 410 510

Logical Operation

Logical operations can be perform on vector. For ex. If we want to find out how many students are having marks greater than 75.

# create vector
MarksInEnglish <- c(77, 80, 69,80,59,40,100)
Students <- c("John","Ravi",”Samual","Nick","Das","Jack","Sam")
names(MarksInEnglish) <- Students
MarksInEnglish >75
Result will be
John Ravi Samual Nick Das Jack Sam
TRUE TRUE FALSE TRUE FALSE FALSE TRUE

In summary Vectors are basic data storing one-dimensional array with elements of same type of elements. We can execute various commands to perform various operations and get desired data. Hope you will like this blog and will give your valuable feedback.

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.