R Lab Programs-1
R Lab Programs-1
R Lab Programs-1
quad <-function(a, b, c)
{
disc <- b*b - 4*a*c
if(disc == 0){
print("Roots are equal and real.")
root1 <- -b %/% (2*a);
print(paste("Root:",root1));
}else if(disc > 0){
print("Roots are real and distinct.");
root1 <- (-b + sqrt(disc))%/%(2*a);
root2 <- (-b - sqrt(disc))%/%(2*a);
print(paste("Root 1:",root1));
print(paste("Root 2:%.",root2));
}else{
print("Roots are complex numbers.");
real <- - b/(2*a);
imag <- sqrt(- disc )%/%(2*a);
print(paste("Root 1:",real,"+i",imag));
print(paste("Root 2:",real,"-i",imag));
}
}
a<-as.integer(readline(prompt="Enter coefficient of a: "))
b<-as.integer(readline(prompt="Enter coefficient of b: "))
c<-as.integer(readline(prompt="Enter coefficient of c: "))
if(a == 0 && b == 0){
print("Invalid input.")
}else{
quad(a, b, c)
}
OUTPUT
Enter coefficient of a: 1
Enter coefficient of b: 2
Enter coefficient of c: 1
[1] "Roots are equal and real."
[1] "Root: -1"
Enter coefficient of a: -1
Enter coefficient of b: 4
Enter coefficient of c: 3
[1] "Roots are real and distinct."
[1] "Root 1: -1"
[1] "Root 2:%. 4"
Enter coefficient of a: 12
Enter coefficient of b: 6
Enter coefficient of c: 8
[1] "Roots are complex numbers."
[1] "Root 1: -0.25 +i 0"
[1] "Root 2: -0.25 -i 0”
LAB PROGRAM – 3
Write a R program that use a loop to find the minimum of a vector
X, without using any predefined functions, like min(), sort (), etc
OUTPUT:
“Minimum value:1”
LAB PROGRAM - 4
Write a R program that generates a random number between 1 and
10/n and prompts the user to get the number. If the user guess is
too high then the program should print too high. If the users guess
is too low then the program should print too low. If the users guess
is correct then print CONGRATULATIONS
OUTPUT:
Guess a number 1 and 10:5
[1] "Too Low"
Guess again6
[1] "Too Low"
Guess again7
[1] "Too Low"
Guess again9
[1] "CONGRATULATIONS"
LAB PROGRAM -5
Write a program that prompts a user to enter a sentence and count
the number of vowels and consonants using a for loop and if loop
only
countvowelsAndconsonants <- function(text){
text <- tolower(text)
for(char in strsplit(text,"")[[1]]){
if(char %in% vowels){
vowel_count <- vowel_count+1
}else if (char >= "a"&& char <="z"){
consonant_count <- consonant_count+1
}
}
cat("Number of vowels:",vowel_count,"\n")
cat("Number of consonants:", consonant_count,"\n")
}
#prompt the user to enter a word or a sentence
text <- readline(prompt = "Enter a sentence:")
#call the function to count vowels and consonants
countvowelsAndconsonants(text)
OUTPUT:
Enter a sentence: hi im apoorva
Number of vowels: 6
Number of consonants: 5
#calculate median
median_age <- median(df$Age)
print(median_age)
OUTPUT:
31.8
35
LAB PROGRAM-7
Write a program in R that defines multiple functions (different
functions )that takes data frames as arguments and returns, mean,
median ,standard deviation respectively, of a specified column. Call
the function with a data frame that contains at least one column
with numeric values.
# Numeric columns
numeric_columns <- sapply(data, is.numeric)
OUTPUT:
"Numeric Column Statistics:"
mean median variance std_dev std_error
Numeric_age 30.4 28 8.030e+01 8.961027 4.007493
Numeric_income 56000.0 55000 1.675e+08 12942.179106 5787.91
8451
Numeric_expenses 33000.0 32000 2.200e+07 4690.415760 2097.61
76
LAB PROGRAM 8
Write a program in r that reads a list of numbers from a file and
replaces any negative values with 0. Use apply familiar functions
and calculate the mean, variance and sd of each column in the data
frame
original_data <- read.table("C:/Users/apoor/Downloads/data2.txt",
header = TRUE)
OUTPUT:
Original Data:
col1 col2 col3
1 3 -2 7
2 -1 5 8
3 4 2 -6
4 -3 0 9
5 6 -4 12
6 2 -7 10
7 9 8 -5
8 -2 11 14
9 -8 6 3
10 7 0 -9
return(stats)
}
# Call functions for each student and populate the result lists
students <- list(student1, student2, student3, student4, student5,
student6, student7, student8, student9, student10)
OUTPUT
Names
1 ALICE
2 BOB
3 CHARLIE
4 DAVID
5 EMMA
6 FRANK
7 GRACE
8 HENRY
9 IVY
10 JACK
[1] "Mean Age: 24.5"
[1] "Median Age: 22.5"
[1] "Mean Height: 170.6"
[1] "Median Height: 171"
LAB PROGRAM- 10
Write a program in R to create student records, create an S3 class
Student Record for objects that are a list with a named elements
‘name’ ‘Subject completed’ ‘grades’ ‘credit’. Write a student record
method for the generic function mean, which return weighted gpa,
with subjects weighted by credit also write a student record
method for print, which employs formatting and arranging subjects
by year code. Create another class for a cohort of students and
write methods for mean and print the mean for every student in
the cohort.
return(mean_gpa)
}
# Create the print method for studentRecord
print.studentRecord <- function(x, ...) {
cat("Name:", x$name, "\n")
cat("Subjects Completed:", x$subjects_completed, "\n")
cat("Grades:", x$grades, "\n")
cat("Credit:", x$credit, "\n")
}
return(mean_gpa)
}
OUTPUT:
Mean GPA: NA
$name
[1] "John Doe"
$subjects_completed
[1] "Math" "English" "Science"
$grades
[1] 80 90 85
$credit
[1] 3 4 3
$students[[1]]$subjects_completed
[1] "Math" "English" "Science"
$students[[1]]$grades
[1] 80 90 85
$students[[1]]$credit
[1] 3 4 3
$students[[2]]
$students[[2]]$name
[1] "John Doe"
$students[[2]]$subjects_completed
[1] "Math" "English" "Science"
$students[[2]]$grades
[1] 80 90 85
$students[[2]]$credit
[1] 3 4 3