Vectors are the basic way to store many values in R. They keep data in order and make it easy to work with numbers, words, or logical values.
0
0
Why vectors are the fundamental data structure in R Programming
Introduction
When you want to store a list of numbers like ages or temperatures.
When you need to keep a series of words like names or colors.
When you want to do math or comparisons on many values at once.
When you want to select or change parts of a list easily.
When you want to organize data simply before making tables or charts.
Syntax
R Programming
c(value1, value2, value3, ...)
The c() function combines values into a vector.
All values in a vector must be the same type (all numbers, or all text, etc.).
Examples
This creates a numeric vector with four numbers.
R Programming
numbers <- c(10, 20, 30, 40)
This creates a character vector with three words.
R Programming
words <- c("apple", "banana", "cherry")
This creates a logical vector with true/false values.
R Programming
flags <- c(TRUE, FALSE, TRUE)
Sample Program
This program creates two vectors: one for ages and one for names. It prints both vectors and then calculates and prints the average age.
R Programming
ages <- c(25, 30, 22, 40) names <- c("Anna", "Bob", "Cara", "Dan") # Print ages vector print(ages) # Print names vector print(names) # Calculate average age average_age <- mean(ages) print(average_age)
OutputSuccess
Important Notes
Vectors are simple but powerful for storing data in R.
Mixing types in a vector will convert all values to the most flexible type (usually text).
Many R functions expect vectors as input, so understanding them helps with most tasks.
Summary
Vectors hold many values of the same type in order.
They are easy to create and use with the c() function.
Vectors are the building blocks for more complex data in R.