Welcome to R

Introduction to R programming Language

Image for post
Image for post
Photo by Steinar Engeland on Unsplash
> 3 + 2
[1] 5
> log10(100)
[1] 2

Creating a variable

> data_1 = 3
> data.2 <- 4
> Data_1 = 5
> data_1
[1] 3
> Data_1
[1] 5

Basic Arithmetic Operations

> sqrt(9)
[1] 3
> abs(2^3-3^2)
[1] 1
> cos(2*pi) + cos(pi)
[1] 0
> log2(64)
[1] 6
> log(64) # treated as ln(64)
[1] 4.158883
> choose(4,2) # 4!/((4-2)!*2!)
[1] 6

Matrix

Combine Function

> data = c(1,2,3,4)
> data
[1] 1 2 3 4
> v = c(1,2,3,4,5,6)
> data = matrix(v,nrow = 3, ncol = 2, byrow = T,dimnames = list(c("I","II","III"),c("A","B"))
> data
A B
I 1 2
II 3 4
III 5 6
> data_2 = matrix()
> data_2
[,1]
[1,] NA
#As you can see without any parameter, default parameter were used.
>> data_3 = matrix(v)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
# byrow parameter is false as default. So values are inserted by column and since number of columns are not specified it takes default value as 1 for ncol.

Vector

> v = c(1,-1,1)
> length(v)
[1] 3
> prod(v)
[1] -1
> sort(v)
[1] -1 1 1
> cumsum(v) # 1, 1+(-1)=0,0+1=1 => 1,0,1
[1] 1 0 1
> cumprod(v) # 1, 1*(-1)=-1, -1*(1) = -1 => 1,-1,-1
[1] 1 -1 -1
> diff(v) # (-1)-1=-2,1-(-1)=2 => -2,2
[1] -2 2
> v = c(3,5,6,8)
> diff(v) # 5-3,6-5,8-6
[1] 2 1 2
> diff(v, lag=2) # 6-3,8-5
[1] 3 3
> diff(v, differences = 2) # 5-3,6-5,8-6 => 2,1,2 => 1-2,2-1
[1] -1 1

Sequences

> 1:4
[1] 1 2 3 4
> v = 1:4
> v
[1] 1 2 3 4
> seq(1,4,by=2)# start from 1 and increment by 2
[1] 1 3
> seq(1,5,by=3)
[1] 1 4
> seq(1,5,length=3)
[1] 1 3 5
> rep(0,4)
[1] 0 0 0 0
> matrix(rep(1,4),2)
[,1] [,2]
[1,] 1 1
[2,] 1 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store