User:Psk48
1) Write a R program for different types of data structures in R. numeric_vector <- c(1.2, 2.5, 3.7, 4.1) character_vector <- c("apple", "banana", "cherry") logical_vector <- c(TRUE, FALSE, TRUE) matrix_data <- matrix(1:12, nrow = 3, ncol = 4) my_list <- list(name = "John", age = 30, hobbies = c("reading", "swimming")) df <- data.frame(
Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 22), Score = c(95, 88, 75)
) array_data <- array(1:24, dim = c(3, 4, 2)) print("Numeric Vector:") print(numeric_vector) print("Character Vector:") print(character_vector) print("Logical Vector:") print(logical_vector) print("Matrix:") print(matrix_data) print("List:") print(my_list) print("Data Frame:") print(df) print("Array:") print(array_data)
2) Write a R program that include variables, constants, datatypes.
name <- "John"
age <- 30
height <- 175.5
is_student <- TRUE
PI <- 3.14159265359
MAX_SCORE <- 100
cat("Name:", name, "\n")
cat("Age:", age, "\n")
cat("Height:", height, "cm\n")
cat("Is Student:", is_student, "\n")
cat("Constant PI:", PI, "\n")
cat("Constant MAX_SCORE:", MAX_SCORE, "\n")
name_type <- class(name)
age_type <- class(age)
height_type <- class(height)
is_student_type <- class(is_student)
pi_type <- class(PI)
max_score_type <- class(MAX_SCORE)
cat("Data Type of 'name':", name_type, "\n")
cat("Data Type of 'age':", age_type, "\n")
cat("Data Type of 'height':", height_type, "\n")
cat("Data Type of 'is_student':", is_student_type, "\n")
cat("Data Type of 'PI':", pi_type, "\n")
cat("Data Type of 'MAX_SCORE':", max_score_type, "\n")
a) Write a R program that include different operators.
a <- 10
b <- 5
addition <- a + b
subtraction <- a - b
multiplication <- a * b
division <- a / b
modulo <- a %% b
cat("Arithmetic Operators:\n")
cat("Addition:", addition, "\n")
cat("Subtraction:", subtraction, "\n")
cat("Multiplication:", multiplication, "\n")
cat("Division:", division, "\n")
cat("Modulo:", modulo, "\n\n")
x <- 10
y <- 15
greater_than <- x > y
less_than <- x < y
equal_to <- x == y
not_equal_to <- x != y
cat("Comparison Operators:\n")
cat("Greater Than:", greater_than, "\n")
cat("Less Than:", less_than, "\n")
cat("Equal To:", equal_to, "\n")
cat("Not Equal To:", not_equal_to, "\n\n")
- Logical Operators
p <- TRUE q <- FALSE logical_and <- p & q logical_or <- p | q logical_not <- !p cat("Logical Operators:\n") cat("Logical AND:", logical_and, "\n") cat("Logical OR:", logical_or, "\n") cat("Logical NOT:", logical_not, "\n\n") num <- 5 num <- num + 2 cat("Assignment Operator:\n") cat("Value of 'num' after addition:", num, "\n\n") string1 <- "Hello, " string2 <- "World!" concatenated_string <- paste(string1, string2) cat("Concatenation Operator:\n") cat("Concatenated String:", concatenated_string, "\n\n")
b) Write a R program that include different, control structures.
grade <- 85
if (grade >= 90) {
cat("Grade: A\n")
} else if (grade >= 80) {
cat("Grade: B\n")
} else if (grade >= 70) {
cat("Grade: C\n")
} else {
cat("Grade: F\n")
} cat("\nCounting from 1 to 5 using a for loop:\n") for (i in 1:5) {
cat(i, " ")
} count <- 1 cat("Numbers from 1 to 10 using a while loop:\n") while (count <= 10) {
cat(count, " ") count <- count + 1
} cat("\n")
c) Write a R program that includes default arguments. greet_user <- function(name = "Guest", time = "morning") {
message(paste("Good", time, ",", name, "!"))
} greet_user() greet_user("Alice", "evening") 4) A) Write a R program for quicksort implementation. quicksort <- function(arr) {
if (length(arr) <= 1) { return(arr) } pivot <- arr[1] lesser <- arr[arr < pivot] equal <- arr[arr == pivot] greater <- arr[arr > pivot] sorted_lesser <- quicksort(lesser) sorted_greater <- quicksort(greater) return(c(sorted_lesser, equal, sorted_greater))
} arr <- c(4, 2, 7, 1, 9, 3) sorted_arr <- quicksort(arr) cat("Original Array: ", arr, "\n") cat("Sorted Array: ", sorted_arr, "\n") B)binary tree Node=function(key){
list( key=key, left=NULL, right=NULL )
} insert=function(root,key){
if(is.null(root)){ return(Node(key))
}
if(key < root$key){ root$left=insert(root$left,key) }else if(key > root$key){ root$right=insert(root$right,key)
} return(root) } inorder=function(root){
if(!is.null(root)){ inorder(root$left) cat(root$key,) inorder(root$right)
} } bst=NULL bst=insert(bst,50) bst=insert(bst,30) bst=insert(bst,20) bst=insert(bst,40) bst=insert(bst,70) bst=insert(bst,60) bst=insert(bst,80) inorder(bst) cat('inorder traversal of bst')
5) Write a R program for calculating cumulative sums, and products minima maxima and calculus. numeric_vector <- c(2, 4, 6, 8, 10) cumulative_sum <- cumsum(numeric_vector) product <- prod(numeric_vector) minimum_value <- min(numeric_vector) maximum_value <- max(numeric_vector) mean_value <- mean(numeric_vector) median_value <- median(numeric_vector) cat("Original vector: ", paste(numeric_vector, collapse = ", "), "\n") cat("Cumulative sum: ", paste(cumulative_sum, collapse = ", "), "\n") cat("Product: ", product, "\n") cat("Minimum value: ", minimum_value, "\n") cat("Maximum value: ", maximum_value, "\n") cat("Mean value: ", mean_value, "\n") cat("Median value: ", median_value, "\n")
6) Write a R program that include linear algebra operations on vectors and matrices. vector_a <- c(2, 4, 6) vector_b <- c(1, 3, 5) matrix_A <- matrix(c(1, 2, 3, 4), nrow = 2) matrix_B <- matrix(c(5, 6, 7, 8), nrow = 2) cat("Vector A:\n", vector_a, "\n") cat("Vector B:\n", vector_b, "\n") cat("Matrix A:\n") print(matrix_A) cat("Matrix B:\n") print(matrix_B) vector_sum <- vector_a + vector_b cat("Vector Addition (A + B):\n", vector_sum, "\n") vector_diff <- vector_a - vector_b cat("Vector Subtraction (A - B):\n", vector_diff, "\n") scalar <- 2 vector_scalar_mult <- scalar * vector_a cat("Vector Scalar Multiplication (2 * A):\n", vector_scalar_mult, "\n") vector_div <- vector_a / vector_b cat("Vector Division (A / B):\n", vector_div, "\n") matrix_sum <- matrix_A + matrix_B cat("Matrix Addition (A + B):\n") print(matrix_sum) matrix_diff <- matrix_A - matrix_B cat("Matrix Subtraction (A - B):\n") print(matrix_diff) if (ncol(matrix_A) == nrow(matrix_B)) {
matrix_mult <- matrix_A %*% matrix_B cat("Matrix Multiplication (A * B):\n") print(matrix_mult)
} else {
cat("Matrix multiplication is not possible due to non-conformable dimensions.\n") }
7) Write a R program for finding stationary distribution of markanov chains. install.packages("markovchain") library(markovchain) transition_matrix <- matrix(c(0.8, 0.2, 0.3, 0.7), byrow = TRUE, nrow = 2, dimnames = list(c("State A", "State B"), c("State A", "State B"))) my_chain <- new("markovchain", states = c("State A", "State B"), transitionMatrix = transition_matrix) stationary_distribution <- steadyStates(my_chain) cat("Stationary Distribution:\n") print(stationary_distribution)
8) Write a R program for any visual representation of an object with creating graphs using graphic functions: Plot(), Hist(), Linechart(),Pie(), Boxplot(), Scatterplots(). data <- data.frame(
Name = c("Alice", "Bob", "Charlie", "David", "Eva"), Age = c(25, 30, 22, 28, 35), Salary = c(50000, 60000, 45000, 55000, 70000)
) barplot(data$Salary, names.arg = data$Name, main = "Salary Comparison", xlab = "Name", ylab = "Salary") hist(data$Age, main = "Age Distribution", xlab = "Age", ylab = "Frequency", col = "lightblue") plot(data$Age, data$Salary, type = "o", main = "Age vs. Salary", xlab = "Age", ylab = "Salary", col = "blue") slices <- c(30, 20, 10, 15, 25) names <- c("A", "B", "C", "D", "E") pie(slices, labels = names, main = "Pie Chart") boxplot(data$Salary, main = "Salary Boxplot", ylab = "Salary", col = "orange") plot(data$Age, data$Salary, main = "Age vs. Salary", xlab = "Age", ylab = "Salary", pch = 16, col = "green")
9) Write a R-program for with any data set containing data frame objects, indexing and sub-setting data frames, and employ manipulating and analysing data. data(mtcars) head(mtcars) selected_columns <- mtcars[, c("mpg", "hp", "wt")] head(selected_columns) filtered_data <- mtcars[mtcars$mpg > 20, ] head(filtered_data) sorted_data <- mtcars[order(mtcars$mpg), ] head(sorted_data) mtcars$mpg_per_wt <- mtcars$mpg / mtcars$wt head(mtcars) summary(mtcars) plot(mtcars$hp, mtcars$mpg, xlab="Horsepower", ylab="Miles per Gallon", main="Scatter Plot of MPG vs. HP") write.csv(mtcars, file="modified_mtcars.csv", row.names=FALSE)
10) Write a program to create an any application of Linear Regression in multivariate context for predictive purpose. data(mtcars) set.seed(123) # For reproducibility sample_indices <- sample(1:nrow(mtcars), 0.7 * nrow(mtcars)) # 70% for training, 30% for testing train_data <- mtcars[sample_indices, ] test_data <- mtcars[-sample_indices, ] lm_model <- lm(mpg ~ ., data = train_data) # Predict mpg using all other variables predictions <- predict(lm_model, newdata = test_data) comparison <- data.frame(Actual = test_data$mpg, Predicted = predictions) head(comparison)