R Vector

Vectors are the most basic data type in R language.

Atomic vectors have 6 types: logical, integer, double, character, complex, and raw.

Creating Vectors

Below we create vectors with a single element, which can be any of the above 6 types:

Example

# Character atomic vector
print("example");

# Double-precision 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 produces the following result:

[1] "example"
[1] 12.5
[1] 23
[1] TRUE
[1] 2+3i
[1] 68 65 6c 6c 6f

Below we create a vector with multiple elements, using a colon between numbers: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, discard it
v <- 3.8:11.4
print(v)

Executing the above code produces the following result:

[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 series operatorseq()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 produces the following result:

[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 type:

Example

# Numeric and logical values will be converted to character type
s <- c('apple','red',5,TRUE)
print(s)

Executing the above code produces the following result:

[1] "apple" "red"   "5"     "TRUE" 

Accessing Vector Elements

Vector elements can be accessed using square brackets[]The index starts from 1 (unlike some other programming languages). If the index is negative, the element at that position is removed; TRUE, FALSE, or 0 and 1 can also be used.

Example

# Access vector elements using index
t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
u <- t[c(2,3,6)]
print(u)

# Use logical index: TRUE means read, FALSE means not read
v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
print(v)

# The second and fifth elements will be deleted
x <- t[c(-2,-5)]
print(x)

# Use 0/1 index: 1 means read, 0 means not read
y <- t[c(0,0,0,0,0,0,1)]
print(y)

Executing the above code produces the following result:

[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 on two vectors of the same length, and the result is also output as 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 produces the following result:

[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 have different lengths, the shorter one will recycle its own elements until it matches 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)

sub.result <- v1-v2
print(sub.result)

Executing the above code produces the following result:

[1] 6 14 12 11 5 18 [1] -4 2 2 -1 -5 6

Vector Sorting

We can use the sort() function to sort vectors:

Example

v <- c(2,11,6,5,0,21, -7, 111)

# Sort
sort.result <- sort(v)
print(sort.result)

# The decreasing parameter is set to TRUE for descending order, and defaults to FALSE for ascending order
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

# Sort character type
v <- c("Example","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 produces the following result:

[1]  -7   0   2   5   6  11  21 111
[1] 111  21  11   6   5   2   0  -7
[1] "Facebook" "Google"   "Example"   "Zhihu"   
[1] "Zhihu"    "Example"   "Google"   "Facebook"
Other Extensions