R vector
Vectors are the most basic data type in R.
There are six types of atomic vectors: logical, integer, double, character, complex, and raw.
Creating Vectors
Below, we create single-element vectors, which can be any of the six types mentioned:
Example
# Character atomic vector
print("tutorialpro")
# Double atomic vector
print(12.5)
# Integer atomic vector
print(23L)
# Logical atomic vector
print(TRUE)
# Complex atomic vector
print(2+3i)
# Raw atomic vector
print(charToRaw('hello'))
Executing the above code outputs:
[1] "tutorialpro"
[1] 12.5
[1] 23
[1] TRUE
[1] 2+3i
[1] 68 65 6c 6c 6f
Next, we create vectors with multiple elements using the colon :
operator:
Example
# Create a series from 5 to 13
v <- 5:13
print(v)
# Create a series from 6.6 to 12.6
v <- 6.6:12.6
print(v)
# If the last element does not belong to the series, it is discarded
v <- 3.8:11.4
print(v)
Executing the above code outputs:
[1] 5 6 7 8 9 10 11 12 13
[1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6
[1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8
We can also use the sequence operator seq() to create vectors.
The following example creates a vector from 5 to 9, incrementing by 0.4:
print(seq(5, 9, by = 0.4))
Executing the above code outputs:
[1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0
The c() function can convert non-character elements to character:
Example
# Numeric and logical elements will be converted to character type
s <- c('apple', 'red', 5, TRUE)
print(s)
Executing the above code outputs:
[1] "apple" "red" "5" "TRUE"
Accessing Vector Elements
Vector elements can be accessed using brackets []
, with indices starting from 1 (unlike other programming languages). Negative indices will remove elements at those positions, and TRUE, FALSE, or 0 and 1 can also be used.
Example
# Accessing vector elements using indices
t <- c("Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat")
u <- t[c(2, 3, 6)]
print(u)
# Using logical indices, TRUE means read, FALSE means do not read
v <- t[c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE)]
print(v)
# The second and fifth elements will be removed
x <- t[c(-2, -5)]
print(x)
# Using 0/1 indices, 1 means read, 0 means do not read
y <- t[c(0, 0, 0, 0, 0, 0, 1)]
print(y)
Executing the above code outputs:
[1] "Mon" "Tue" "Fri"
[1] "Sun" "Fri"
[1] "Sun" "Tue" "Wed" "Fri" "Sat"
[1] "Sun"
Vector Operations
We can perform addition, subtraction, multiplication, or division operations on two vectors of the same length, and the result will also be a vector.
Example
# Create two vectors
v1 <- c(3, 1, 4, 5, 0, 12)
v2 <- c(5, 11, 9, 8, 1, 22)
# Addition
add.result <- v1 + v2
print(add.result)
# Subtraction
sub.result <- v1 - v2
print(sub.result)
# Multiplication
multi.result <- v1 * v2
print(multi.result)
# Division
divi.result <- v1 / v2
print(divi.result)
Executing the above code outputs:
[1] 8 12 13 13 1 34
[1] -2 -10 -5 -3 -1 -10
[1] 15 11 36 40 0 264
[1] 0.60000000 0.09090909 0.44444444 0.62500000 0.00000000 0.54545455
Vector Recycling
If two vectors are of unequal length, the elements of the shorter vector will be recycled to match the length of the longer vector.
Example
v1 <- c(1, 8, 7, 5, 0, 12)
v2 <- c(5, 6)
# v2 becomes c(5, 6, 5, 6, 5, 6)
add.result <- v1 + v2
print(add.result)
Executing the above code outputs:
[1] 6 14 12 11 5 18
print(add.result)
sub.result <- v1 - v2
print(sub.result)
Executing the above code outputs the following results:
Vector Sorting
We can use the sort()
function to sort vectors:
Example
v <- c(2, 11, 6, 5, 0, 21, -7, 111)
# Sorting
sort.result <- sort(v)
print(sort.result)
# The `decreasing` parameter set to TRUE for descending order, default is FALSE for ascending order
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)
# Sorting character types
v <- c("tutorialpro", "Google", "Zhihu", "Facebook")
sort.result <- sort(v)
print(sort.result)
# Descending order
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)
Executing the above code outputs the following results:
[1] -7 0 2 5 6 11 21 111
[1] 111 21 11 6 5 2 0 -7
[1] "Facebook" "Google" "tutorialpro" "Zhihu"
[1] "Zhihu" "tutorialpro" "Google" "Facebook"