Acl 7-9 in R
Acl 7-9 in R
Aim:
To create a list and access its elements in R.
Algorithm:
Step 1: Create a list with multiple elements.
Step 2: Access and print the elements of the list.
Program:
my_list <- list(name = "John", age = 30, city = "New York")
print("List Elements:")
print(my_list$name) # accessing the "name" element
print(my_list$age) # accessing the "age" element
print(my_list$city) # accessing the "city" element
Output:
[1] "List Elements:"
[1] "John"
[1] 30
[1] "New York"
Result:-
Aim:
To create and modify factors in R.
Algorithm:
Step 1: Create a vector of categorical data.
Step 2: Convert the vector into a factor.
Step 3: Modify the levels of the factor.
Step 4: Print the modified factor.
Program:
category <- c("A", "B", "A", "C", "B", "B")
category_factor <- factor(category)
levels(category_factor) <- c("Group A", "Group B", "Group C")
print("Modified Factor:")
print(category_factor)
Output:
[1] "Modified Factor:"
[1] Group A Group B Group A Group C Group B Group B
Levels: Group A Group B Group C
Result:-
Aim:
To add and remove elements from a list in R.
Algorithm:
Step 1: Create an empty list.
Step 2: Add elements to the list using the $ operator or the [[ indexing.
Step 3: Remove an element from the list using the [[ indexing and assignment.
Step 4: Print the modified list.
Program:
my_list <- list()
my_list$name <- "John"
my_list$age <- 30
my_list$name <- NULL
print("Modified List:")
print(my_list)
Output:
[1] "Modified List:"
$age
[1] 30
Result:-
Aim:
To work with factors and levels in R.
Algorithm:
Step 1: Create a vector of categorical data.
Step 2: Convert the vector into a factor.
Step 3:Print the levels of the factor.
Step 4:Reorder the levels of the factor.
Step 5: Print the modified levels.
Program:
category <- c("A", "B", "A", "C", "B", "B")
category_factor <- factor(category)
print("Original Levels:")
print(levels(category_factor))
category_factor <- factor(category_factor, levels = c("B", "A", "C"))
print("Modified Levels:")
print(levels(category_factor))
Output:
[1] "Original Levels:"
[1] "A" "B" "C"
[1] "Modified Levels:"
[1] "B" "A" "C"
Result:-
Algorithm:
Step 1: Define a function named ‘sum_even_numbers’ that takes a parameter n.
Step 2: Initialize a variable result to 0.
Step 3: Use a for loop to iterate from 1 to n.
Step 4: Check if the current number i is divisible by 2 using the modulo operator
(%%).
Step 5: If i is even, add it to the result.
Step 6: Return the final value of result.
Step 7: In the main program, set the value of number to the desired number up to
which you want to calculate the sum of even numbers.
Step 8: Call the ‘sum_even_numbers’ function with number as the argument and store
the result in ‘sum_result’.
Step 9: Print the result using the print function, which displays the sum of even
numbers.
Program:
sum_even_numbers <- function(n)
{result <- 0
for (i in 1:n) {
if (i %% 2 == 0) {
result <- result + i
}
}
return(result)
}
number <- 10
sum_result <- sum_even_numbers(number)
print(paste("The sum of even numbers from 1 to", number, "is", sum_result))
Output:
[1] "The sum of even numbers from 1 to 10 is 30"
Aim:
To reverse a given string using R Program
Algorithm:
Program:
reverse_string <- function(string)
{reversed <- ""
for (i in nchar(string):1) {
reversed <- paste(reversed, substr(string, i, i), sep = "")
}
return(reversed)
}
text <- "Hello, World!"
reversed_text <- reverse_string(text)
print(paste("The reversed string is:", reversed_text))
Output:
[1] "The reversed string is: !dlroW ,olleH"
Result:
Program:
fibonacci_series <- function(n)
{series <- c(0, 1)
if (n <= 2)
{ return(series[1:n]
)
} else {
for (i in 3:n) {
series <- c(series, series[i-1] + series[i-2])
}
return(series)
}
}
19
terms <- 10
fib_series <- fibonacci_series(terms)
Output:
[1] "The Fibonacci series up to 10 terms is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 3
Result:
20
8D) Check Prime Number
Aim:
Algorithm:
{if (n <= 1)
{ return(FALSE)
for (i in 2:sqrt(n))
{if (n %% i == 0)
{return(FALSE)
return(TRUE)
21
}
number <- 17
if (is_prime(number)) {
} else {
Output:
Result:
{for (i in 1:10) {
result <- n * i
number <- 5
multiplication_table(number)
Output:
Result:
24
EXERCISE NO: 9
Aim:
Algorithm:
Step 2: View the structure of the dataset using the str() function.
Step 4: Create a box plot to visualize the distribution of the Sepal Length variable.
Step 5: Create a scatter plot to visualize the relationship between Petal Length and Petal
Width.
Program:
str(iris)
summary(iris)
plot(Petal.Length ~ Petal.Width, data = iris, main = "Scatter Plot - Petal Length vs.
Output:
Sepal.Length Sepal.Width Petal.Length Petal.Width
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
Median :5.800 Median :3.000 Median :4.350 Median :1.300
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
25
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
Species
setosa :50
versicolor:50
virginica :50
Result:
26
9B) Analyzing the mtcars Dataset
Aim:
Algorithm:
Step 2: View the structure of the dataset using the str() function.
Step 4: Create a histogram to visualize the distribution of MPG (Miles per Gallon).
Step 5: Create a scatter plot to visualize the relationship between Horsepower and MPG.
Program:
data(mtcars)
str(mtcars)
summary(mtcars)
hist(mtcars$mpg, main = "Histogram - MPG", xlab = "MPG", ylab = "Frequency",
col = "lightblue")
plot(mpg ~ hp, data = mtcars, main = "Scatter Plot - Horsepower vs. MPG", xlab =
"Horsepower", ylab = "MPG")
Output:
27