A vector is a workhorse datatype of R and basic data structure in R. It contains elements of a homogenous data type. The data types can be logical, integer, double, character, complex or raw.
Creating a vector
numeric_vector <- c(1, 2, 3) character_vector <- c("fruits", "vegetables", "nuts") logical_vector<- c(TRUE,FALSE,T,F)
Attributes of a vector
There are two important attribute of a vector:
- length of the vector
- data type of the vector
A vectors length can be checked with length()
numeric_vector=c(1,2,3) length(numeric_vector) [1] 3
A vector’s type can be checked with the typeof() function
x=c(1,"aa",TRUE,1.5) typeof(x) [1] "character"
Creating a vector using :operator
if we want to create a vector of consecutive numbers, the : operator is very helpful.
x=1:7 y y=-3:3 y [1] 1 2 3 4 5 6 7 [1] -3 -2 -1 0 1 2 3
In R, a function is a piece of code written to carry out a specified task. R Functions are called as objects because you can work with them exactly the same way you work with any other type of object. R Vector functions are those functions which we use in R vectors. For Example: seq()
, rep()
.
Creating a vector using seq() function
More complex sequences can be created using the seq() function, like defining number of points in an interval, or the step size.
seq(1, 4, by=0.2) seq(0,50,by=5) [1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 [1] 0 5 10 15 20 25 30 35 40 45 50
rep() function
rep() replicates the values in x. It is a generic function.
You can use the rep() function in several ways,just by changing the arguments.
To repeat the vector c(1,2,3) five times, use this code:
rep(c(1,2,3),times=5) [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
We can also repeat every value by specifying the argument each, like this:
rep(c(1,0,0),each=2) [1] 1 1 0 0 0 0
We can tell R for each value how often it has to repeat:
rep(c(1,0),times=c(2,3)) [1] 1 1 0 0 0
Naming a vector
We can assign names to vector members.
expenses=c(100,210,200,150,310,200,100) days_vector=c("mon","tue","wed","thurs","fri","sat","sun") days vector names(expenses)=days_vector expenses mon tue wed thurs fri sat sun 100 210 200 150 310 200 100
Combining/appending vectors
Vectors can be combined via the function c
n = c(2, 3, 5) s = c("aa", "bb", "cc", "dd", "ee") c(n, s) [1] "2" "3" "5" "aa" "bb" "cc" "dd" "ee"
Vector Arithmetics
Arithmetic operations are performed member by member in a vector.
for example,suppose we have two vectors x and y.
x=c(1,2,3,4) y=c(1,2,3,4) z=x+y z [1] 2 4 6 8
Then, if we multiply x by 5, we would get a vector with each of its members multiplied by 5.
x*5 [1] 5 10 15 20
Similarly, for subtraction, multiplication, and division, we get new vectors via memberwise operations.
x-y x*y x/y [1] 0 0 0 0 [1] 1 4 9 16 [1] 1 1 1 1
Recycling Rule
If two vectors are of unequal length, the shorter one will be recycled in order to match the longer vector. For example, the following vectors u and v have different lengths, and their sum is computed by recycling values of the shorter vector u.
v=c(1,2,3,4,5,6) a=u+v a [1] 11 22 33 14 25 36
How to access elements of the vector?
We retrieve values in a vector by declaring an index inside a single square bracket “[]” operator.
u[2] u[1:3] [1] 20 [1] 10 20 30