Basic Commands:-
> mytext <- "Hello World!"
> print ( mytext)
[1] "Hello World!"
Here first Statement defines a string variable mytext, Where we assign a string "Hello World!" and then statement print() is being used to print the value store in variable mytext.
Save the above code in a file text.R and execute
$Rscript test.R
Lists:-
A list is an R-object which ca contain many different types of elements inside it like vectors, functions and even another list inside it.
> # Create a list.
> list1 <- list(c(2,5,9),1.2,3,sin)
> #print the list.
> print(list1)
When we Execute the above code , it produces the following results-
[[1]]
[1] 2 5 9
[[2]]
[1] 1.2
[[3]]
[1] 3
[[4]]
function (x) .Primitive("sin")
Array:-
> # Create an array.
> A<- array(c('5','6'),dim =c(3,3,2))
> print(A)
When we Execute the above code , it produces the following results-
, , 1
[,1] [,2] [,3]
[1,] "5" "6" "5"
[2,] "6" "5" "6"
[3,] "5" "6" "5"
, , 2
[,1] [,2] [,3]
[1,] "6" "5" "6"
[2,] "5" "6" "5"
[3,] "6" "5" "6"
Frames:-
Frames are tabular data objects. Unlike a matrix in data frame each column can contain different modes of data. The first column can be numeric while the second column can be character and third column can be logical.
Frames are created using the data.frame() function.
> # Create the Frames.
> BMI <-data.frame(
+ gender = c("Female"," Male", "Female", "Male"),
+ height =c(152,168,157,160),
+ weight =c(36,67,56,47),
+ Age =c(4,25,34,23)
+ )
> print(BMI)
When we Execute the above code , it produces the following results-
gender height weight Age
1 Female 152 36 4
2 Male 168 67 25
3 Female 157 56 34
4 Male 160 47 23
-data>->->->
Addition:-
Add two vectors
> #Addtion
> x<-c(3,4,6)
> y<-c(4,5,7)
> print(x+y)
Result
[1] 7 9 13
Inverse:-
> x <- matrix(c(4, 7, 2, 6), nrow = 2, ncol = 2)
> x_inverse <- solve(x)
> # Print the result
> print(x_inverse)
[,1] [,2]
[1,] 0.6 -0.2
[2,] -0.7 0.4
Multiplication:-
> # Multiplication
> x<-c(3,4,6)
> y<-c(4,5,7)
> print(x*y)
Result
[1] 12 20 42
Traspose:-
%*% This Operator is used to multiply a matrix with its transpose.
> #Transpose
> M=matrix(c(1,5,4,1,12,5),nrow=2,ncol=3,byrow=TRUE)
> t=M%*%t(M)
> print(t)
Result
[,1] [,2]
[1,] 42 81
[2,] 81 170
-data>->->->
Mean:-
Find the mean of 12,15 ,6,7,9,10,17,-15,8,-9.
> #Create a vector.
> x<-c(12,15 ,6,7,9,10,17,-15,8,-9)
> # Find mean
> Result.mean <-mean(x)
> print(Result.mean)
Result
[1] 6
Median:-
Find the median of 12,15 ,6,7,9,10,17,-15,8,-9.
> #Create a vector.
> x<-c(12,15 ,6,7,9,10,17,-15,8,-9)
> # Find median
> Result.median <-median(x)
> print(Result.median)
Result
[1] 8.5
Mode:-
> x<-c(12,5,9,45,21,12,4,6,7)
> getmode <- function(v) {
+ uniqv <- unique(v)
+ uniqv[which.max(tabulate(match(v, uniqv)))]
+ }
> # Find mode
> Result.mode <- getmode(x)
> # Print result
> print(Result.mode)
Result
[1] 12
First, the getmode function determines which values in the vector are unique (uniqv).
After that, it counts the instances of each distinct value using tabulate and match.
which.max returns the position of the value that happens the most, while max returns its position.
Range:-
Find the range of 45, 56, 78, 90, 67, 46, 89, 20.
Code:
x = c(45, 56, 78, 90, 67, 46, 89, 20)
max(x) - min(x)
Result:
[1] 70
Description:
The formula for finding the range is:
Range = Largest value - Smallest value
max(x): The largest value in the data.
min(x): The smallest value in the data.
For this data:
Largest value = 90
Smallest value = 20
Range =
90
−
20
=
70
90−20=70.
Interquartile Range (IQR):-
Find the interquartile range (IQR) of 45, 56, 78, 90, 67, 46, 89, 20, 77, 98.
Step 1: Arrange the data in ascending order:
20
,
45
,
46
,
56
,
67
,
77
,
78
,
89
,
90
,
98
20,45,46,56,67,77,78,89,90,98
Code:
x <- c(20, 45, 46, 56, 67, 77, 78, 89, 90, 98)
IQR(x)
Result:
[1] 37.75
Description:
The interquartile range (IQR) is the difference between the third quartile (Q3) and the first quartile (Q1):
IQR = Q3 - Q1
𝑄
1
Q1: The median of the lower half of the data (first 50%).
𝑄
3
Q3: The median of the upper half of the data (last 50%).
For this data:
𝑄
1
=
46
Q1=46
𝑄
3
=
83.75
Q3=83.75
𝐼
𝑄
𝑅
=
𝑄
3
−
𝑄
1
=
83.75
−
46
=
37.75
IQR=Q3−Q1=83.75−46=37.75.
-data>->->->