R Programing Bhagu
R Programing Bhagu
cat("U15IG21S0401\n")
n=as.numeric(readLines("stdin",n=1))
x=as.numeric(readLines("stdin",n=n))
cat(y)
output:
C:\R>Rscript lab1.R
U15IG21S0401
10
33
12
40
77
10 is even no
33 is odd no
12 is even no
40 is even no
77 is odd no
2.#write a R program to illustrate the for loop with stop on condition, to print
error message
cat("U15IG21S0401\n")
n=as.numeric(readLines("stdin",n=1))
words=readLines("stdin",n=n)
for(w in words){
if(w=="stop"){
cat("word=",w,"length=",nchar(w),"\n")
}
Output:
c:\R>Rscript lab2.R
U15IG21S0401
hi
hello
welcome
stop
world
word= hi length= 2
Error: The program has been stoped due to word present as stop
Execution halted
3.#write a R program to find a factorial of a number using recursion
cat("U15IG21S0401\n")
fact=function(n){
if(n==0){
return(1)
}else{
return(n*fact(n-1))
n=as.numeric(readLines("stdin",n=1))
result=fact(n)
c:\R>Rscript lab3.R
U15IG21S0401
cat("U15IG21S0401\n")
library(tidyverse)
library(broom)
str(employee_data)
summary(anova_result)
# Perform T-test for pairwise comparisons
print(pairwise_tukey)
output:
c:\R>Rscript lab4.R
U15IG21S0401
✔ purrr 1.0.2
── Conflicts
──────────────────────────────────────────────────────────────────
───────────────────────────── tidyverse_conflicts() ──✖ dplyr::filter() masks
stats::filter()
$ employee_id: int 1 2 3 4 5 6 7 8 9
$ salary : int 55000 62000 70000 48000 59000 81000 52000 65000 75000
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
$department
cat("U15IG21S0401\n")
n=as.numeric(readLines("stdin",n=1))
gender=readLines("stdin",n=n)
heights=as.numeric(readLines("stdin",n=n))
result=tapply(heights,gender,mean)
result1=sapply(result,mean)
cat(result)
cat(result1)
output:
c:\R>Rscript lab5.R
U15IG21S0401
female
male
5.3
5.4
5.3 5.4
5.3 5.4
6.#write a R program to find stationary distribution of markov chain
Cat(“U15IG21S0401\n”)
library(markovchain)
n=as.numeric(readLines("stdin",n=1))
states=readLines("stdin",n=n)
probs=as.numeric(readLines("stdin",n=n*n))
trans_matrix=matrix(probs,nrow=n,ncol=n,byrow
=TRUE,dimnames=list(states,states))
mkc<-new("markovchain",states,transitionMatrix=trans_matrix)
stat_distb=steadyStates(mkc)
cat(stat_distb)
output:
c:\R>Rscript lab6.R
U15IG21S0401
Package: markovchain
Version: 0.9.5
BugReport: https://github.com/spedygiorgio/markovchain/issues
sunny
rainy
0.2
0.8
0.4
0.6
cat("U15IG21S0401\n")
quick_sort=function(arr){
if(length(arr)<=1){
return(arr)
pivot=arr[1]
lesser=arr[arr<pivot]
equal=arr[arr==pivot]
greater=arr[arr>pivot]
return(c(quick_sort(lesser),equal,quick_sort(greater)))
binary_search=function(arr,low,high,key){
if(low<=high){
mid=(low+high)%/%2
if(arr[mid]==key){
return(mid)
}else if(key<arr[mid]){
binary_search(arr,low,mid-1,key)
}else if(key>arr[mid]){
binary_search(arr,mid+1,high,key)
}
}else{
return(0)
n=as.numeric(readLines("stdin",n=1))
x=as.numeric(readLines("stdin",n=n))
y=quick_sort(x)
cat(y)
key=as.numeric(readLines("stdin",n=1))
low=1
high=length(y)
pos=binary_search(y,low,high,key)
if(pos==0){
}else{
}
Output:
c:\R>Rscript lab7.R
U15IG21S0401
40
60
22
36
22 36 40 60
n=as.numeric(readLines("stdin",n=1))
rollno=c()
sname=c()
marks=c()
for(i in 1:n){
rno=as.numeric(readLines('stdin',n=1))
name=readLines('stdin',n=1)
m=as.numeric(readLines("stdin",n=1))
rollno=append(rollno,rno)
sname=append(sname,name)
marks=append(marks,m)
df=data.frame(rollno=rollno,sname=sname,marks=marks)
write.csv(df,'students.csv')
df1=read.csv('students.csv')
print(df1)
output:
c:\R>Rscript lab8.R
U15IG21S0401
11 401 Rakshita 90
22 33 Spandana 92
3 3 6 Bhagya 89
9.#write a R program to demonstrate histogram,linechart,scatter plots, pie
chart, bar plot and box plot
cat("U15IG21S0401/n")
#histogram
height=seq(from=3,to=4,by=0.1)
hist(height,xlab="height",ylab="number of persons",main="histogram",col="red")
#piechart
fruits=c("Apple","Banana","Mango")
sales=c(400,300,350)
pie(sales,fruits)
#barplot
ayear=c("2018-19","2019-20","2020-21","2021-22","2022-23")
passout=c(3500,3550,4000,3900,4100)
#linechart
plot(height,type='l',pch=17,cex=2,col="red",xlab="height")
#scatter plot
price=c(300,200,270)
plot(sales,price,pch=17,cex=2,col="black")
#boxplot
boxplot(height)
output:
10.#write a R program to do basic data manipulation and analysis using data
frames
qu
cate pric anli
product gory e ty
Elect 120
laptop onics 0 5
furni
desk ture 250 2
Elect
headphones onics 80 10
furni
chair ture 150 4
furni
smartphone ture 600 3
Elect
bookshelf onics 120 3
furni
mouse ture 20 8
furni
Table ture 180 2
cat("U15IG21S0006\n")
#step1 load the data
df=read.csv("sales.csv")
#step2 explore the data
types of data frame
columns
str(df)
#step3 print few rows of
data frame
head(df)
#step4 know the summary
of each column
summary(df$Quantity)
summary(df$Price)
#step5 filter the rows
df=df[df$Quantity>3,]
df
#step6 create new
columns
df$Total_Price=df$Quanti
ty*df$Price
df
#step7 aggregate function
and grouping
df1=aggregate(df$Total_P
rice,list(df$Category),sum)
df1
#step8 selecting specific
columns from data frame
df2<-
df[,c("Product","Quantity"
)]
df2
#step9 visulaize the data
barplot(df2$Quantity,nam
es.arg =
df2$Product,col="red")
#step10 export the new
data frame
write.csv(df2,"sales.csv")
Output:
c:\R>Rscript lab10.R
U15IG21S0006
> #step1 load the data
> df=read.csv("sales.csv")
$ Quantity: int 5 2 10 4 3 3 8 2
> head(df)
3 Headphone Electronics 80 10
> summary(df$Quantity)
> summary(df$Price)
> df=df[df$Quantity>3,]
> df
3 Headphone Electronics 80 10
7 Mouse Electronics 20 8
> df$Total_Price=df$Quantity*df$Price
> df
> df1=aggregate(df$Total_Price,list(df$Category),sum)
> df1
Group.1 x
1 Electronics 6960
2 Furniture 600
> df2<-df[,c("Product","Quantity")]
1 Laptop 5
3 Headphone 10
4 Chair 4
11.#Lab 11 WARP TO create any application of linear
Regression
years_experienc
employee_id e education_level job_title salary
Software
1 5 Bachelor's Engineer 80000
2 3 Master's Data Scientist 95000
Senior Research
3 8 PhD Scientist 120000
Marketing
4 2 Bachelor's Analyst 65000
5 1 Associate's Sales Associate 40000
6 6 Master's Project Manager 90000
Chief Technology
7 10 PhD Officer 150000
Software
8 4 Bachelor's Engineer 85000
9 2 Master's Data Scientist 70000
Senior Research
10 7 Bachelor's Scientist 100000
cat("U15IG21S0401\n")
#in multivariate context for predictive purpose
#assuming ur dataset is in a csv file named "employee1.csv"
employees <- read.csv("employee1.csv")
#view the structure of the dataset
str(employees)
#check for missing values
summary(employees)
#visualize relationship b/n variables(eg.scatter plots box)
#this step helps u understand how the variables relate to each
other
#scatter plot of salary v/s years of experiance
plot(salary ~ years_experience, data=employees,main="Scatter
Plot: Salary vs Years of Experience")
#scatter plot of salary v/s education level(using box plots)
boxplot(salary ~ education_level, data=employees, main="Box
Plot: Salary by Education Level")
#assuming u want to predict "salary" based on
"years_experiance"& "educational_level & "job_title"
model<-lm(salary ~ years_experience + education_level +
job_title,data=employees)
#get a summary of the model's output
summary(model)
#extraxt & print specific information
cat("Intercept:",model$coefficients[1],"\n")
cat("Coefficient for years_experience:",model$coefficients[2],"\
n")
cat("Coefficient for educational_level (Bachelor's as
reference):",model$coefficients[3],"\n")
cat("Coefficient for job_title (Software Engineer as
reference):",model$coefficients[4],"\n")
cat("R-squared:", summary(model)$r.squared,"\n")
output:
c:\R>Rscript lab11.R
U15IG21S0401
'data.frame': 10 obs. of 5 variables:
$ employee_id : int 1 2 3 4 5 6 7 8 9 10
$ years_experience: int 5 3 8 2 1 6 10 4 2 7
$ education_level : chr "Bachelor's" "Master's" "PhD"
"Bachelor's" ...
$ job_title : chr "Software Engineer" "Data Scientist"
"Senior Research Scientist" "Marketing Analyst" ...
$ salary : int 80000 95000 120000 65000 40000 90000
150000 85000 70000 100000
employee_id years_experience education_level job_title
Min. : 1.00 Min. : 1.00 Length:10 Length:10
1st Qu.: 3.25 1st Qu.: 2.25 Class :character Class :character
Median : 5.50 Median : 4.50 Mode :character
Mode :character
Mean : 5.50 Mean : 4.80
3rd Qu.: 7.75 3rd Qu.: 6.75
Max. :10.00 Max. :10.00
salary
Min. : 40000
1st Qu.: 72500
Median : 87500
Mean : 89500
3rd Qu.: 98750
Max. :150000
Call:
lm(formula = salary ~ years_experience + education_level +
job_title,
data = employees)
Residuals:
1 2 3 4 5 6 7
-7.500e+03 7.500e+03 2.274e-13 -9.095e-13 -1.819e-12 -
2.046e-12 2.046e-12
8 9 10
7.500e+03 -7.500e+03 2.046e-12
Intercept: 30000
Coefficient for years_experience: 10000
Coefficient for educational_level (Bachelor's as reference):
10000
Coefficient for job_title (Software Engineer as reference): -
4.352441e-10
R-squared: 0.9728015
12.#write a R program to find mean, median and mode
cat("U15IG21S0401\n")
cat("Enter the number of students")
n=as.numeric(readLines("stdin",n=1))
cat("Enter the family income of",n,"students\n")
income=as.numeric(readLines("stdin",n=n))
avg=mean(income)
med=median(income)
x=table(income)
y=names(x)
result=sort(x,decreasing = TRUE)
z=max(x)
mod=paste(y[1],z)
cat("The mean of family income=",avg,"\n")
cat("The median of family income or middle value=",med,"\n")
cat("The mode or most frequent income is=",mod)
output:
c:\R>Rscript lab12.R
U15IG21S0401
Enter the number of students5
Enter the family income of 5 students
2000
3000
5600
3000
560
The mean of family income= 2832
The median of family income or middle value= 3000
The mode or most frequent income is= 560 2