Dealing with vectors in R

Dealing with vectors in R

Creating Vectors in R:

Vectors are the most basic R data objects and there are six types of atomic vectors. They are logical, integer, double, complex, character and raw.

Single element vector:

> x=’a’                   

> print(x)

[1] “a”                     # character type

> x=1+2i

> print(x)

[1] 1+2i                   # complex type

> x=11.26

> print(x)

[1] 11.26                 # integer type

> x=TRUE

> print(x)

[1] TRUE                               # logical type

Multiple element vector:

There are may ways to create vector in R. The commonly used techniques are:

  1. Using colon operator: 
> v=2:10
> print(v)
[1]  2  3  4  5  6  7  8  9 10
> v=1.8:10.8
> print(v)
 [1]  1.8  2.8  3.8  4.8  5.8  6.8  7.8  8.8  9.8 10.8
> v=1.5:10.1
> print(v)
[1] 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5
> v=1.8:10.5
> print(v)
[1] 1.8 2.8 3.8 4.8 5.8 6.8 7.8 8.8 9.8
 

2. Using seq() operator:

> print(seq(2, 10, by = 0.5))   #Create vector with elements from 2 to 10 incrementing by 0.5.
 
 [1]  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0  7.5  8.0  8.5  9.0  9.5
[17] 10.0
  • 3. Using c() function:
> v=c(1,2,5,9,3)
> print(v)
[1] 1 2 5 9 3

If one of the elements in the vector is character, then the numeric and logical elements are converted into character type.

> v=c("white",5,8,TRUE)
> print(v)
[1] "white" "5"     "8"     "TRUE"

Accessing the elements of a vector:

> v=c(1,29,2,6,8,4)
To access the vector-elements we use the vector indexing method. Vector indexing can be done in 3 ways:
Using integer vector as index:
In R unlike most programming language the index starts from 1. [ ] are used for indexing. Giving negative integer values drops the corresponding value of the vector.
 
> v[1]                                      
[1] 1
> v[c(1,3,6)]
[1] 1 2 4
> v[c(-1)]
[1] 29  2  6  8  4

Logical indexing:

> v[v>5]                      # filtering vectors based on conditions

[1] 29  6  8
 
> v[v<5]
[1] 2  4
 

III.     Modifying a vector:

We can modify a vector using the assignment operator.

> v=c(1,29,2,6,8,4)

> v[3]=0                                #Modifying 3rd element of the vector

> print(v)

[1]  1 29  0  6  8  4

 

> v[v<5]=2                            # Modifying elements less than 5 of the vector

> print(v)

[1]  2 29  2  6  8  2

 

> v=v[1:4]                             #truncate first 4 elements of vector v

> print(v)

[1]  2 29  2  6

Matrix:

I)          Creating Vector Matrix in R: 

> matrix(c(1:16),nrow=4,ncol=4)
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
 

II) Eigenvalues and Eigenvectors:

> A=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3)

> A

     [,1] [,2] [,3] [1,]    1    4    7

[2,]    2    5    8

[3,]    3    6    9

> det(A)==0

[1] TRUE

> A=matrix(c(1,2,3,4,5,6,7,8,10),nrow=3)

> det(A)==0

[1] FALSE

> E=eigen(A)

> names(E)

[1] “values”  “vectors”

> E$values

[1] 16.7074933 -0.9057402  0.1982469

> E$vectors

           [,1]       [,2]       [,3] [1,] -0.4524587 -0.9369032  0.1832951

[2,] -0.5545326 -0.1249770 -0.8624301

[3,] -0.6984087  0.3264860  0.4718233

> det(E$vectors)

[1] -0.9593596

 

Mathematica-City

Mathematica-city is an online Education forum for Science students run by Kounteyo, Shreyansh and Souvik. We aim to provide articles related to Actuarial Science, Data Science, Statistics, Mathematics and their applications using different Statistical Software. We also provide freelancing services on the aforementioned topics. Feel free to reach out to us for any kind of discussion on any of the related topics.