R Tutorial menu

R Vectors

A vector is a list of values of the same type, and it is the most important building block in R.

Creating a Vector

Use c(), which stands for combine:

Example

r
marks <- c(78, 85, 62, 90, 74)
marks
length(marks)

cities <- c("Karachi", "Lahore", "Quetta")
cities
Output
[1] 78 85 62 90 74
[1] 5
[1] "Karachi" "Lahore"  "Quetta" 

length() tells you how many values a vector holds.

In R, even a single value like 5 is a vector of length one. That is why every result starts with [1].

All Values Have One Type

A vector holds only one type. If you mix types, R changes them all to the most flexible type, usually text:

Example

r
c(1, "two", TRUE)
Output
[1] "1"    "two"  "TRUE"

If a number column suddenly shows quotes, check for one text value hidden among the numbers. It turns the whole vector into text.

Quick Ways to Make Vectors

CodeResultMeaning
1:51 2 3 4 5A sequence of whole numbers
seq(0, 100, by = 25)0 25 50 75 100Steps of a chosen size
rep("Pass", 3)"Pass" "Pass" "Pass"Repeat a value

Example

r
1:10
seq(0, 100, by = 25)
Output
 [1]  1  2  3  4  5  6  7  8  9 10
[1]   0  25  50  75 100

Picking Items by Position

Use square brackets. In R, positions start at 1, not 0:

Example

r
fruits <- c("mango", "banana", "apple", "guava")
fruits[1]
fruits[c(2, 4)]
fruits[-1]
fruits[length(fruits)]
Output
[1] "mango"
[1] "banana" "guava" 
[1] "banana" "apple"  "guava" 
[1] "guava"

A minus sign removes that position. fruits[-1] means "everything except the first item".

Changing and Adding Items

Example

r
stock <- c(5, 12, 0)
stock[3] <- 20
stock <- c(stock, 7)
stock
Output
[1]  5 12 20  7

Maths on a Whole Vector

This is where R shines. An operation works on every value at once, with no loop:

Example

r
prices <- c(120, 250, 80)
prices * 2
prices + c(10, 20, 30)
sum(prices)
Output
[1] 240 500 160
[1] 130 270 110
[1] 450

Useful functions for numeric vectors:

FunctionWhat it gives
sum()The total
mean()The average
median()The middle value
max(), min()The largest and smallest
sort()The values in order
round()Rounded values

Example

r
marks <- c(78, 85, 62, 90, 74)
mean(marks)
sort(marks, decreasing = TRUE)
Output
[1] 77.8
[1] 90 85 78 74 62

Picking Items by Condition

A comparison on a vector gives a vector of TRUE and FALSE. Put it inside brackets to keep only the TRUE items:

Example

r
marks <- c(78, 85, 62, 90, 74)
marks > 75
marks[marks > 75]
sum(marks > 75)
which(marks == 90)
Output
[1]  TRUE  TRUE FALSE  TRUE FALSE
[1] 78 85 90
[1] 3
[1] 4

sum() on TRUE/FALSE values counts the TRUEs, so sum(marks > 75) tells you how many students scored above 75. which() gives the positions.

Named Vectors

You can give each value a name:

Example

r
sales <- c(Mon = 4500, Tue = 3800, Wed = 5200)
sales
sales["Tue"]
Output
 Mon  Tue  Wed 
4500 3800 5200 
 Tue 
3800 

Missing Values

Real data often has gaps. R marks them with NA. Most maths functions return NA if any value is missing, unless you add na.rm = TRUE:

Example

r
x <- c(10, NA, 30)
mean(x)
mean(x, na.rm = TRUE)
is.na(x)
Output
[1] NA
[1] 20
[1] FALSE  TRUE FALSE

Exercise

Exercise

What does fruits[-1] return if fruits <- c("mango", "banana", "apple")?

Learn R with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp