Categories: Big Data

Learn R – What are Vectors – Code Examples

This article represents high level concepts in relation with Vector data type in R programming language along with code samples. For those new to R language, it should be noted that R provides a console-based platform to perform analysis on data. R can be seen as a programming language for data scientist.
Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are the key points described later in this article:

  • What are Vectors?
  • Vectors – Code Examples

 

What are Vectors?
Vector, in R, can be defined as a collection of things of same data type. Simply speaking, it can be looked upon as a list of things of same data type. For example, a collection of numbers, say, odd numbers such as 1, 3, 5, 7, 9 or character sets such as “apple”, “google”, “microsoft”. Vectors can be of following different types:
  • Integer
  • Numeric
  • Character
In R, “c” is used to represent the collection. “c” at times is considered as short form of “concatenate” or “combination” or “collection”.
For example, c(1,3,5,7,9) or c(“apple”, “google”, “microsoft”). Following is how it is represented in R.
oddnos = c( 1,3,5,7,9)
companies = c( "apple", "google", "microsoft")

 

Vectors – Code Examples

Consider an example of doctors with following details:

  • Name
  • Age
  • Salary

Following code example displays command to store each of the above data and how to access them. Pay attention to the fact that vector of one type store data of same type. Following code example demonstrate vector of types such as following:

  • Character
  • Integer
  • Numeric
# Character Vector representing collection of doctors' names
docNames <- c( "chris", "calvin", "scott" );
# Numeric Vector representing collection of doctors' salary
docSalary <- c( 67345.50, 89765, 123432 );
# Integer Vector representing collection of doctors' age
docAge <- c( 25, 29, 27 );

# Access doctors' names
docNames # Displays [1] chris, calvin, scott
docNames [1] # Displays [1] chris

# Vector storing docNames and docAge
docSummary <- c( docNames, docAge );

# Display details as String; Pay attention that all data is converted as character vector
docSummary # Displays [1] "chris", "calvin", "scott", "25", "29", "27"

 

Latest posts by Abhimanyu Shukla (see all)
Abhimanyu Shukla

Abhimanyu has been working extensively on different microsoft technologies for past 7-8 years.

Recent Posts

Questions to Ask When Thinking Like a Product Leader

This blog represents a list of questions you can ask when thinking like a product…

3 days ago

Three Approaches to Creating AI Agents: Code Examples

AI agents are autonomous systems combining three core components: a reasoning engine (powered by LLM),…

1 week ago

What is Embodied AI? Explained with Examples

Artificial Intelligence (AI) has evolved significantly, from its early days of symbolic reasoning to the…

2 months ago

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

5 months ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

5 months ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

5 months ago