R Tutorial

 Hi, Well come to Fahad Hussain Free Computer Education Here you can learn Complete computer Science, IT related course absolutely Free! As we know R Programming is one the statistical language which used to solve different types of statistical problem and machine learning project. The powerful packages make it more powerful to solve problem. The whole course base on the concept of theory and Practical to handle these kind of problem and prerequisite for solving these issues.

For further Assistance and code visit: https://fahadhussaincs.blogspot.com/

For Complete course YouTube Channel: https://www.youtube.com/channel/UCapJ...


To Download Slides and Data Set Click Here.

Tutorial No. 01:
Click to WATCH the Series of Videos

print('Hi, Fahad Hussain')

Tutorial No. 02:
Click to WATCH the Series of Videos

a <- 5.23
print(a)
print(class(a))
print(typeof(a))

b <- 45L
print(b)
print(class(b))
print(typeof(b))

c <- 'Fahad'
print(c)
print(class(c))
print(typeof(c))

d <- TRUE
print(d)
print(class(d))
print(typeof(d))

e <- 4 + 3i
print(e)
print(class(e))
print(typeof(e))

f <- charToRaw('abc')
print(f)
print(class(f))
print(typeof(f))

z <- 45
y = as.integer(z)
print(y)
print(is.integer((z)))

Tutorial No. 03:
Click to WATCH the Series of Videos

myName <- readline(prompt = "Enter Your Name")
myGender <- readline(prompt = "Enter Your Gende")
myAge <- readline(prompt = "Enter Your Age")

myAge = as.integer((myAge))

print(myName)
print(myGender)
print(myAge)

Tutorial No. 04:
Click to WATCH the Series of Videos

a = 0
b = -5

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a %% b)
print(a %/% b)
print(a ^ b)

print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
print(a == b)
print(a != b)

print(a && b)
print(a || b)

vec1 = c(1,2,-3,45,0)
vec2 = c(12,0,33,4,25)

print(vec1 & vec2)
print(vec1 | vec2)
print(!vec1)


a = 12
b <- 23
cc <<- 55

12 -> e
55 ->> f

print(a)
print(b)
print(cc)
print(e)
print(f)


zz = 2:15
print(zz)

print(30 %in% zz)
print(30 %*% zz)

Tutorial No. 05:
Click to WATCH the Series of Videos

a <- 1:10
a
b = seq(1,10)
b
c = seq(1,5,by=2)
c

vect1 <- c(1,2,3,477,32,45,78)
vect1
typeof(vect1)
class(vect1)


vect2 <- c(1,2,3,477,32,45,78)
vect2 <- as.integer(vect2)
typeof(vect2)
class(vect2)

vect3 <- c('Fahad',"Hussain","CS",12, 45L)
vect3
typeof(vect3)
class(vect3)

a = 1
b = 3
vect4 <- c(a<b,b<a)
vect4
typeof(vect4)
class(vect4)


# Operations in Vectors

vect1 <- c(1,2,3)
vect2 <- c(4,2,6)

merge = c(vect1, vect2)
merge

print(vect1 + vect2)
print(vect1 - vect2)
print(vect1 * vect2)
print(vect1 / vect2)
print(vect1 %% vect2)

#indexing

vect1[-1]

#duplicate index
vect1[c(1,2,2,3)]


#Range indexes
vect3 <- c(1,2,3,54,56,6,7,8,89,9,5)
vect3[2:7]

#Out of order indexes
vect3 <- c(1,2,3,54,56,6,7,8,89,9,5)
vect3[2:7]
vect3[c(2,1,3,4,5,12)]

#Named Vector 
vect5 = c("R Programming","Data Science")
vect5

names(vect5) = c("Starting Stage","Ending Stage")
vect5

vect5["Starting Stage"]

Tutorial No. 06:
Click to WATCH the Series of Videos

vec<-c(3,4,5,6)
char_vec<-c("Fahad","Hussain","CS","tutorial")
logic_vec<-c(TRUE,FALSE,FALSE,TRUE)

out_list<-list(vec,char_vec,logic_vec)
out_list


out_list[1]
out_list[2]
out_list[3]

list1 <- list('Fahad',"Hussain", 2, 2.56, 76L, TRUE, 3+7i)
list1
class(list1)
typeof(list1)


list_record <- list(c("Fahad","Adam","John"),
                    matrix(c(50,55,65,80,90,100),
                     nrow = 2), list("C#","R","Python"))

list_record

names(list_record) <- c("Students", "Marks", "Courses")
list_record


list_record["Courses"]

#list convert vector
v <- unlist(list_record)
v

#merging list
l1 <- list(1,2,32)
l2 <- list(1,2,32)
mergelist <- list(l1, l2)
mergelist


#Manipulate 
list_record[1] <- "StudentsNames"
list_record

list_record[1] <- NULL
list_record

list_record[1]

Tutorial No. 07:
Click to WATCH the Series of Videos

v1 <- c(1,2,3)
v2 <- c(11,22,33,44,55,95)

arr <- array(c(v1,v2),dim = c(3,3,2))
print(arr)
typeof(arr)
class(arr)

l1 <- list(1,2,3)
l2 <- list(11,22,33,44,55,95)

arr <- array(list(l1,l2),dim = c(3,3,2))
print(arr)
typeof(arr)
class(arr)

v11 <- c(1,2,3)
v22 <- c(11,22,33,44,55,95)

col <- c("Col1","Col2","Col3")
row <- c("Row1","Row2","Row3")
mat <- c("Matrix1","Matrix2")

arr <- array(c(v11,v22),dim = c(3,3,2),dimnames = list(row,col,mat))

print(arr)
typeof(arr)
class(arr)
arr
arr[1:3]


result1 <- arr[1:3]
result2 <- arr[2:3]
merge <- result1 + result2
merge

arr[1] = 100
arr


vec1 <-c(1,2,3)  
vec2 <-c(1,1,1,1,1,1)  

res1 <- array(c(vec1,vec2),dim=c(3,3,2))  
print(res1)  

result <- apply(res1,1,sum)  
print(result) 

Tutorial No. 08:
Click to WATCH the Series of Videos

matrix <- matrix(c(1,2,3,4,22,3,4,5), nrow = 2, ncol = 4,
                byrow = TRUE)
print(matrix)
print(class(matrix))
print(typeof(matrix))

matrix1 <- matrix(c(1:8), nrow = 2, ncol = 4,
                 byrow = TRUE)

print(matrix1)

rownames <- c('Row1','Row2')
colnames <- c('Col1','Col2','Col3','Col4')

matrix2 <- matrix(c(1:8), nrow = 2, ncol = 4,
                  byrow = TRUE, dimnames= list(rownames,colnames))
matrix2

#to access
print(matrix2[2,3])
print(matrix2[1,])
print(matrix2[,3])

#to change the specific value in matrix

matrix2[2,3] <- 77
matrix2

matrix2[matrix2 > 5] <- 0
matrix2

#to add row
rbind(matrix2,c(22,22,22,22))

# to add col
cbind(matrix2,c(99,89))

#Modifiy
print(matrix2)
dim(matrix2) <- c(4,2)
print(matrix2)


matrix1 <- matrix(c(1,2,3,4,22,3,4,5), nrow = 2, ncol = 4,
                 byrow = TRUE)
matrix2 <- matrix(c(1,12,23,4,22,33,4,45), nrow = 2, ncol = 4,
                 byrow = TRUE)

print(matrix1 + matrix2)
print(summary(matrix1))

Tutorial No. 09:
Click to WATCH the Series of Videos

vec1 <- c(1,22,33,100,1)
vec2 <- c('Fahad','CS','Fahad','Hussain','Ali')

print(vec1)
print(is.factor(vec1))

fact <- as.factor(vec2)
print(fact)
print(class(fact))
print(typeof(fact))

fact2 <- factor(vec1)
fact2


print(fact[2])
print(fact[c(1,3)])
print(fact[-1])


fact[2] <- "ABC"
print(fact)


Tutorial No. 10:
Click to WATCH the Series of Videos

Emp <- data.frame(
  EmployeeId = c(12,3,34,4,5),
    EmployeeName = c('Fahad','Ali','John','Adam','Sara'),
    EmployeeSalary = c(4500,6500,2400,9800,4700))

print(Emp)
print(class(Emp))
print(typeof(Emp))

# Data Frame Structure

str(Emp)

#Extrct 
result <- data.frame(Emp$EmployeeName)
print(result)

Emp
row <- Emp[c(1:3),c(2:4)]
print(row)

# Row Add in Data Frame
Emp
out <- list(6,'Raheel',7000)
rbind(Emp,out)

# Col Add in Data Frame
put <- c('Karachi','Lahore','Multan','Sakkar','Isl')
cbind(Emp, Address=put)

# Delete Row and Col from Data Frame
Emp
Emp <- Emp[-1,]
print(Emp)

Emp
Emp$EmployeeSalary <- NULL
print(Emp)

print(summary(Emp))


Tutorial No. 11:
Click to WATCH the Series of Videos

age <- 20
birthyear <- 2000
if(age >= 18)
{
  print("Age is greater than 18 now!")
  if(birthyear >= 2001){
    print("True Nested Condition")
  }
  
  else {
    print("False Nested Condition")
  }
  
} else if(age == 11){
  print("Age is Not greater than 18 now!")
} else if(age == 12){
  print("Age is Not greater than 18 now!")
} else if(age == 13){
  print("Age is Not greater than 18 now!")
} else {
  print("Default!")
}


Tutorial No. 12:
Click to WATCH the Series of Videos

# ifelse function()
age <- c(22,31,23,42,34,45,34)
ifelse(age%%2==0,"Yes","No")

y <- 2
x <- switch(
  y,
  "One",
  "Two",
  "Three",
  "Four",
  )
print(x)

val1 = 23
val2 = 12

val3 = "a"
result <- switch(
  val3,
  "a"=cat("Addition = ",val1 + val2),
  "s"=cat("Subts. = ",val1 - val2),
  "m"=cat("Mul. = ",val1 / val2),
  "d"=cat("Div. = ",val1 * val2),
  "m"=cat("Mod. = ",val1 %% val2),
  "p"=cat("Pow. = ",val1 ^ val2),
)
print(result)


x = "a"
y = "b"
a <- switch(
  paste(x,y,sep = ""),
  "ac"="Hi R",
  "ab"="Hi Python",
  "ad"="Hi C",
)
print(a)


Tutorial No. 13:
Click to WATCH the Series of Videos

a <- c(1,2,4,5,76,4,23,5,6,6)
sum = 0;
for (i in a) {
  sum = sum + i
  #print(i)
}
print(sum)

a <- seq(1:10)

for (i in a) {
  
  print(i)
  for (j in a) {
    print(j)
  }
}


a <- 1
while(a<=10){
  print(a)
  a = a + 1
  
  b <- 1
  while(b==10)
  {
    print(b)
    b = b + 1
  }
}



stop <- 1
repeat{
  stop = stop + 1
  print("Hi, Fahad hussain CS")
  if(stop == 5)
  {
    break;
  }
}


d <- seq(1:10)
for (i in d) {
 
  if(i == 6)
  {
    next;
  }
  print(i)
}



Tutorial No. 14:
Click to WATCH the Series of Videos

myFunction1 <- function(){
  sum <- 0
  for (i in seq(1:10)) {
    sum = sum + i
  }
  print(sum)
}

myFunction1()

myFunction2 <- function(){
  a <- c(1,2,3,4,556,6,7,78)
  return(a)
}

myFunction2

myFunction3 <- function(a, b){
  return(a+b)
}

myFunction3(12,12)



print("Hi, Fahad Hussain")
cat("Hi, Fahad Hussain")
p <- c(1,2,3,3445,56,64)
me = mean(p)
print(me)
z <- c(1,2,3,4,5,6)
range(z)

Tutorial No. 15:
Click to WATCH the Series of Videos

plot(1,4)
plot(c(1,2,3,4),c(11,12,13,14))

v1 <- c(1,2,3,4,5)
v2 <- c(11,12,13,14,20)

plot(v1, v2, xlab = "Exp", ylab = "Salary",main = "Salay Chart",
     col="green",type="o",cex=3,pch=24, lty=5)
lines(c(1,2,3,4,5),c(15,17,18,19,20), col="red")


data <- read.csv('DataSet.csv')

png(file ="C:\\Users\\FahadHussain\\Pictures\\salarygraph.png")
plot(data$Age, data$Salary, xlab = "Exp", ylab = "Salary",main = "Salay Chart",
     col="red")

graphics.off()


Tutorial No. 16:
Click to WATCH the Series of Videos

# ScatterPlot 

line1 <- c( 14,64,73,84,95)
line2 <- c(11,12,13, 14, 15)

line3 <- c( 16,50,63,73,91)
line4 <- c(12,18,19,20,23)

plot(line1, line2, xlab = "Line1", ylab = "Line2", main = "ScatterPlot",
     xlim = c(14,95),  ylim = c(11,23))
points(line3, line4,col="red",xlim = c(14,95),  ylim = c(11,23) )


data1 <- read.csv("DataSet.csv")
png(file="C:\\Users\\FahadHussain\\Pictures\\sc.png")
plot(data1$Age, data1$Salary, xlab = "Line1", ylab = "Line2", main = "ScatterPlot")
graphics.off()


Tutorial No. 17:
Click to WATCH the Series of Videos

# BarChart
h <- c(12, 52, 34,31,16)
m <- c("Jan","Feb","Mar","Apr","May")
barplot(h, xlab = "Production", ylab = "Months", main = "total Production",
        names.arg = m,border = "red", col= c('red','green','blue','gray','orange'))
legend("topright",m,cex=0.4, fill = c('red','green','blue','gray','orange'))


m <- c("Jan","Feb","Mar","Apr","May","June","Jul","Aug","Sep","Oct")

data2 <- read.csv("DataSet.csv")
png(file="C:\\Users\\FahadHussain\\Pictures\\bar.png")

barplot(data2$Age, xlab = "Production", ylab = "Months", main = "total Production",
        names.arg = m,border = "red", col= c('red','green','blue','gray','orange'))
legend("topright",m,cex=0.4, fill = c('red','green','blue','gray','orange'))

graphics.off()


Tutorial No. 18
Click to WATCH the Series of Videos

# Histogram in R
v <- c(12, 24, 16,52,13,74,82,92,48,18,55)

hist(v, xlab = "Production", ylab = "Months", main = "total Production",
     border = "red", breaks = 10)


data3 <- read.csv("DataSet.csv")
hist(data3$Age, xlab = "Production", ylab = "Months", main = "total Production",
     border = "red", breaks = 10)


Tutorial No. 19:
Click to WATCH the Series of Videos

# Boxplot in R

v1 <- c(1,2,3,4,5,6,7,8,9,10)
v2 <- c(1:5)
v3 <- c(2,4,5,6)
boxplot(v1,v2, v3,xlab = "Production", ylab = "Months", main = "total Production",
        col=c("red","green","blue"))

v1 <- c(1,2,3,4,5,6,7,8,9,10)
v2 <- c(1:5)
v3 <- c(2,4,5,6)
boxplot(v1,v2, v3,xlab = "Production", ylab = "Months", main = "total Production",
        col=c("red","green","blue"), notch= TRUE, varwidth = TRUE)


Tutorial No. 20:
Click to WATCH the Series of Videos

#Pic Chart in R

x <- c(21, 62, 10, 53)
labels <- c("Pakistan", "India", "Iran", "USA")
pie(x,labels, col = rainbow(length(x)))
png(file = "city.png")
graphics.off()


End of R Programming get ready for Machine Learning Course using R programming... 

No comments:

Post a Comment

Fell free to write your query in comment. Your Comments will be fully encouraged.