R Vectors
A vector is the most basic data type in R.
There are 6 types of atomic vectors: logical, integer, double, character, complex, and raw.
Create a vector
Below we create vectors with a single element, which can be any of the 6 types above:
Example
Character atomic vector
(
"runoob"
)
;
Double atomic vector
(
12.5
)
Integer atomic vector
(
23L
)
Logical atomic vector
(
TRUE
)
Complex atomic vector
(
2
3i
)
Raw atomic vector
(
charToRaw
(
'hello'
)
)
The output of the code above is:
[1] "runoob"
[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 the colon : operator between numbers:
Example
Create a series from 5 to 13
v
<-
5
:
13
(
v
)
Create a series from 6.6 to 12.6
v
<-
6.6
:
12.6
(
v
)
If the last element does not belong to the series, it is discarded
v
<-
3.8
:
11.4
(
v
)
The output of the code above is:
[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 a vector.
In the following example, we create a vector from 5 to 9 in increments of 0.4:
print(seq(5, 9, by = 0.4))
The output of the code above is:
[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 characters:
Example
Numbers and logical values will be converted to the character type
s
<-
c
(
'apple'
,
'red'
,
5
,TRUE
)
(
s
)
The output of the code above is:
[1] "apple" "red" "5" "TRUE"
Accessing vector elements
Vector elements can be accessed using square brackets []; index values start at 1 (unlike other programming languages). If an 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 an index
t
<-
c
(
"Sun"
,
"Mon"
,
"Tue"
,
"Wed"
,
"Thurs"
,
"Fri"
,
"Sat"
)
u
<-
t
[
c
(
2
,
3
,
6
)
]
(
u
)
Using a logical index, TRUE means read, FALSE means do not read
v
<-
t
[
c
(
TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE
)
]
(
v
)
The second and fifth elements are removed
x
<-
t
[
c
(
2
,
5
)
]
(
x
)
Using a 0/1 index, 1 means read, 0 means do not read
y
<-
t
[
c
(
0
,
0
,
0
,
0
,
0
,
0
,
1
)
]
(
y
)
The output of the code above is:
[1] "Mon" "Tue" "Fri"
[1] "Sun" "Fri"
[1] "Sun" "Tue" "Wed" "Fri" "Sat"
[1] "Sun"
Vector operations
We can perform operations such as 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
)
Sum
add.
result
<-
v1
v2
(
add.
result
)
V subtraction
sub.
result
<-
v1
v2
(
sub.
result
)
Multiply
multi.
result
<-
v1
v2
(
multi.
result
)
Divide
divi.
result
<-
v1
/
v2
(
divi.
result
)
The output of the code above is:
[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
Recycling vectors
If two vectors have different lengths, the shorter one recycles 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
(
add.
result
)
sub.
result
<-
v1
v2
(
sub.
result
)
The output of the code above is:
[1] 6 14 12 11 5 18 [1] -4 2 2 -1 -5 6
Sorting vectors
We can use the sort() function to sort a vector:
Example
v
<-
c
(
2
,
11
,
6
,
5
,
0
,
21
,
7
,
111
)
Sorting
sort.
result
<-
sort
(
v
)
(
sort.
result
)
The decreasing parameter: TRUE sets descending order; the default FALSE gives ascending order
revsort.
result
<-
sort
(
v, decreasing
=
TRUE
)
(
revsort.
result
)
Sorting character types
v
<-
c
(
"Runoob"
,
"Google"
,
"Zhihu"
,
"Facebook"
)
sort.
result
<-
sort
(
v
)
(
sort.
result
)
Descending
revsort.
result
<-
sort
(
v, decreasing
=
TRUE
)
(
revsort.
result
)
The output of the code above is:
[1] -7 0 2 5 6 11 21 111
[1] 111 21 11 6 5 2 0 -7
[1] "Facebook" "Google" "Runoob" "Zhihu"
[1] "Zhihu" "Runoob" "Google" "Facebook"