Introduction to basic components of R programming, overview of visualization, data types, basics of plotting graphs, different types of graphs in analytics
# Define variables with different data types
id <- 58
name <- "Vivek Kashyap"
isMarried <- FALSE
dob <- as.Date("2003-08-03")
# printing the values of variables
print(id)
## [1] 58
print(name)
## [1] "Vivek Kashyap"
print(isMarried)
## [1] FALSE
print(dob)
## [1] "2003-08-03"
# Data structures in R
vector_ds <- c(10, 20, 30, 40, 50, 60)
matrix_ds <- matrix(c(11, 13, 15, 12, 14, 16),nrow =2, ncol =3, byrow = TRUE)
dataframe_ds <- data.frame(
Name=c("Vivek","Rahul","sanjay","Abhishek"),
Subject=c("Maths","Physics","Chemistry","Maths"),
Marks=c(58,68,34,89)
)
# displaying data structures values
# Vector data structure
print(vector_ds)
## [1] 10 20 30 40 50 60
# matrix data structure
print(matrix_ds)
## [,1] [,2] [,3]
## [1,] 11 13 15
## [2,] 12 14 16
# data frame data structure
print(dataframe_ds)
## Name Subject Marks
## 1 Vivek Maths 58
## 2 Rahul Physics 68
## 3 sanjay Chemistry 34
## 4 Abhishek Maths 89
# R program to add two vectors of integers type
vect1 <- c(10, 20, 30, 40)
vect2 <- c(50, 60, 70, 80)
resultant_vector <- c(vect1,vect2)
print(resultant_vector)
## [1] 10 20 30 40 50 60 70 80
# R program to find the Sum, Mean, and Product of a Vector.
vect <- c(1, 2, 3, 4, 5)
size <- length(vect)
sum <- 0
product <- 1
for(i in 1:size){
sum <- sum+vect[i]
product <- product*vect[i]
}
avg <- sum/size
# printing sum
print(sum)
## [1] 15
# printing average
print(avg)
## [1] 3
# printing product
print(product)
## [1] 120
# R program to find the minimum and the maximum of a Vector
vec <- c(4:12)
min(vec)
## [1] 4
max(vec)
## [1] 12
# R program to create a list containing strings, numbers, vectors, and a logical
mylist <- list("Vivek", "58", c(1:4), TRUE)
print(mylist)
## [[1]]
## [1] "Vivek"
##
## [[2]]
## [1] "58"
##
## [[3]]
## [1] 1 2 3 4
##
## [[4]]
## [1] TRUE
# R program to create a list containing a vector, a matrix, and a list and give names to the elements in the list. Access the first and second elements of the list
vlist <- list(vect=c(2:9), mat=matrix(c(1:9), nrow=3, ncol=3), lst=list("india",1,TRUE))
# accessing first element of list
print(vlist[1])
## $vect
## [1] 2 3 4 5 6 7 8 9
# accessing second element of list
print(vlist[2])
## $mat
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
# R program to create a matrix of size 3x5
m1 <- matrix(c(1:15), nrow=3, ncol=5)
print(m1)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 4 7 10 13
## [2,] 2 5 8 11 14
## [3,] 3 6 9 12 15
# R program to access the element at 3rd column and 2nd row, only the 3rd row and only the 4th column of a given matrix.
# 3rd column and 2nd row
print(m1[2, 3])
## [1] 8
# 3rd row and 4th column
print(m1[3, 4])
## [1] 12
# Insert multiple rows in R DataFrame
name <- c("sonu","monu","rahul")
age <- c(20, 18, 19)
my_frame <- data.frame(name, age)
print(my_frame)
## name age
## 1 sonu 20
## 2 monu 18
## 3 rahul 19
# adding rows to dataframe
df2 <- data.frame(name=c("suraj","radhika"), age=c(17, 16))
new_frame <- rbind(my_frame, df2)
print(new_frame)
## name age
## 1 sonu 20
## 2 monu 18
## 3 rahul 19
## 4 suraj 17
## 5 radhika 16
# Adding column to dataframe in R?
print(my_frame)
## name age
## 1 sonu 20
## 2 monu 18
## 3 rahul 19
address <- c("Hyd","Chennai","Delhi")
my_frame <- cbind(my_frame, address)
print(my_frame)
## name age address
## 1 sonu 20 Hyd
## 2 monu 18 Chennai
## 3 rahul 19 Delhi
# Extract first N rows from dataframe in R
head(new_frame, 2)
## name age
## 1 sonu 20
## 2 monu 18
# Sort DataFrame by column name in R
print(new_frame)
## name age
## 1 sonu 20
## 2 monu 18
## 3 rahul 19
## 4 suraj 17
## 5 radhika 16
new_frame[order(new_frame$age), ]
## name age
## 5 radhika 16
## 4 suraj 17
## 2 monu 18
## 3 rahul 19
## 1 sonu 20