R Programming basics
Quick notes on R Datatypes typeof(x) or class(x)– both returns the data type of variable. however, typeof(x) is tend to be more specific x>>Numeric print(typeof(x)) >>>double length(x)– returns the length of a vector (list) or other objects: 1, or any other positive integer number. print(x)– prints the value of x in console R Objects 1.Vector: one-dimensional array containing elements of the same data type. apple >>red, green, yellow 2.List: ordered collection of elements that can be of different data types. list1 >> [,1] [,2] [,3] [1,] "a" "a" "b" [2,] "c" "b" "a" 4.Array: a multi-dimensional data structure where all elements must be of the same data type. a > gender height weight Age 1 Male 152.0 81 42 2 Male 171.5 93 38 3 Female 165.0 78 26 Operators 1.+: adds two vectors. v [1] 10.0 8.5 10.0 2.-: Subtracts second vector from the first v [1] -6.0 2.5 2.0 3.*****: Multiplies both vectors v [1] 16.0 16.5 24.0 4./: Divide the first vector with the second v [1] 0.250000 1.833333 1.500000 5.%%: Give the remainder of the first vector with the second v [1] 2.0 2.5 2.0 6.%/%: The result of division of first vector with second (quotient) v [1] 0 1 1 7.^: The first vector raised to the power of second vector v [1] 256.000 166.375 1296.000 Relational Operators 1.>: Checks if each element of the first vector is greater than the corresponding element of the second vector v >[1] FALSE TRUE FALSE FALSE 2.[1] "truth is found the second time" For Loop A For loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. a=c(1:5) for ( i in a) { print(i) } >> 1 2 3 4 5 While Loop The While loop executes the same code again and again until a stop condition is met. v ="Hello, while loop" cnt = 2 while (cnt < 7) { print(v) cnt = cnt + 1 } >> [1] "Hello, while loop" [1] "Hello, while loop" [1] "Hello, while loop" [1] "Hello, while loop" [1] "Hello, while loop" Next: to skip the current iteration of a loop without terminating it. Break: the loop is immediately terminated
Quick notes on R
Datatypes
typeof(x) or class(x)– both returns the data type of variable. however, typeof(x) is tend to be more specific
x<-23.5
print(class(x))
>>>Numeric
print(typeof(x))
>>>double
length(x)– returns the length of a vector (list) or other objects: 1, or any other positive integer number.
print(x)– prints the value of x in console
R Objects
1.Vector: one-dimensional array containing elements of the same data type.
apple <- c('red','green',"yellow") #all character data types
print(apple)
>>>red, green, yellow
2.List: ordered collection of elements that can be of different data types.
list1 <- list(c(2,5,3),21.3,5L) #contains vector, numeric, integer
print(list1)
3.Matrix: a two-dimensional array where all elements must be of the same data type.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE) print(M)
>>>
[,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "a"
4.Array: a multi-dimensional data structure where all elements must be of the same data type.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
[,1] [,2] [,3]
[1,] "green" "yellow" "green"
[2,] "yellow" "green" "yellow"
[3,] "green" "yellow" "green"
, , 2
[,1] [,2] [,3]
[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"
5.Data Frame: a two-dimensional, table-like structure where each column can hold data of a different type, making it similar to a spreadsheet or a SQL table.
BMI<-data.frame( gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26) )
print(BMI)
>>>
gender height weight Age
1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26
Operators
1.+: adds two vectors.
v <-c( 2,5.5,6)
t <-c(8, 3, 4)
print(v+t)
>>[1] 10.0 8.5 10.0
2.-: Subtracts second vector from the first
v <-c( 2,5.5,6)
t <-c(8, 3, 4)
print(v-t)
>>[1] -6.0 2.5 2.0
3.*****: Multiplies both vectors
v <-c( 2,5.5,6)
t <-c(8, 3, 4)
print(v*t)
>>[1] 16.0 16.5 24.0
4./: Divide the first vector with the second
v <-c( 2,5.5,6)
t <-c(8, 3, 4)
print(v/t)
>>[1] 0.250000 1.833333 1.500000
5.%%: Give the remainder of the first vector with the second
v <-c( 2,5.5,6)
t <-c(8, 3, 4)
print(v%%t)
>>[1] 2.0 2.5 2.0
6.%/%: The result of division of first vector with second (quotient)
v <-c( 2,5.5,6)
t <-c(8, 3, 4)
print(v%/%t)
>>[1] 0 1 1
7.^: The first vector raised to the power of second vector
v <-c( 2,5.5,6)
t <-c(8, 3, 4)
print(v^t)
>>[1] 256.000 166.375 1296.000
Relational Operators
1.>: Checks if each element of the first vector is greater than the corresponding element of the second vector
v <-c(2,5.5,6,9)
t <-c(8,2.5,14,9)
print(v>t)
>>[1] FALSE TRUE FALSE FALSE
2.<: Checks if each element of the first vector is less than the corresponding element of the second vector.
v <-c(2,5.5,6,9)
t <-c(8,2.5,14,9)
print(v < t)
>>[1] TRUE FALSE TRUE FALSE
3.==: Checks if each element of the first vector is equal to the corresponding element of the second vector.
v <-c(2,5.5,6,9)
t <-c(8,2.5,14,9)
print(v == t)
>>[1] FALSE FALSE FALSE TRUE
4.<=: Checks if each element of the first vector is less than or equal to the corresponding element of the second vector.
v <-c(2,5.5,6,9)
t <-c(8,2.5,14,9)
print(v<=t)
>>[1] TRUE FALSE TRUE TRUE
5.!=: Checks if each element of the first vector is unequal to the corresponding element of the second vector.
v <-c(2,5.5,6,9)
t <-c(8,2.5,14,9)
print(v!=t)
>>[1] TRUE TRUE TRUE FALSE
Miscellaneous Operators
1.:: Colon operator. It creates the series of numbers in sequence for a vector.
v <-1:8
print(v)
>>[1] 1 2 3 4 5 6 7 8
2.%in%: This operator is used to identify if an element belongs to a vector.
v1 <-8
v2 <-12
t <-1:10
print(v1 %in% t)
print(v2 %in% t)
>>[1] TRUE
[1] FALSE
- %*%: This operator is used to multiply a matrix with its transpose.
M = matrix( c(2,6,5,1,10,4), nrow= 2,ncol = 3,byrow = TRUE)
Q = M %*% t(M)
print(Q)
>>
[,1] [,2]
[1,] 65 82
[2,] 82 117
Condition
x <- c("what","is","truth")
if("Truth" %in% x) {
print("Truth is found the first time")
} else if ("truth" %in% x) {
print("truth is found the second time")
} else { print("There is no Truth in your code ")
}
>>[1] "truth is found the second time"
For Loop
A For loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
a=c(1:5)
for ( i in a)
{
print(i)
}
>>
1
2
3
4
5
While Loop
The While loop executes the same code again and again until a stop condition is met.
v ="Hello, while loop"
cnt = 2
while (cnt < 7) {
print(v)
cnt = cnt + 1
}
>>
[1] "Hello, while loop"
[1] "Hello, while loop"
[1] "Hello, while loop"
[1] "Hello, while loop"
[1] "Hello, while loop"
Next: to skip the current iteration of a loop without terminating it.
Break: the loop is immediately terminated