[go: up one dir, main page]

0% found this document useful (0 votes)
12 views11 pages

Write A R Program For Different Types of Data Structures

The document contains a series of R programming examples covering various data structures, operators, control structures, and algorithms. It includes implementations for quicksort using binary search trees, cumulative calculations, Markov chains, linear algebra operations, and visual representations of data. Additionally, it demonstrates data manipulation and analysis using data frames, along with a linear regression application for predictive purposes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Write A R Program For Different Types of Data Structures

The document contains a series of R programming examples covering various data structures, operators, control structures, and algorithms. It includes implementations for quicksort using binary search trees, cumulative calculations, Markov chains, linear algebra operations, and visual representations of data. Additionally, it demonstrates data manipulation and analysis using data frames, along with a linear regression application for predictive purposes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Write a R program for different types of data structures

#vector
x <- c(1, 3, 5, 7, 8)
print(x)
#list
empid <- c(1, 2, 3, 4)
empName <- c("Raj3", "Veer", "John", "Ashley")
NumberofEmp <- 4
emplist <- list(empid, empName, NumberofEmp)
print(emplist)
#data frame
Name <- c("Amul", "Raj", "Ashish")
language <- c("R", "C++", "Python")
Age <- c(22, 21, 23)
df <- data.frame(Name, language, Age)
print(df)
#matrix
A <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow =
TRUE)
print(A)
#factor
fac <- factor(c("Male", "female", "male", "female", "male"))
print(fac)
2. Write a R program that includes variables, constants and data
types

x=13.8
print(x)
a=TRUE
print(a)
print(class(a))
A=14L
print(A)
print(class(A))
x=13.4
print(x)
print(class(x))
alphabet="a"
print(alphabet)
print(class(alphabet))
message="Welcome to R programming"
print(message)
print(class(message))
x=15L
print(typeof (x))
print(class (x))
complex_value=3i+2;
print(class(complex_value))
3.Write a R program that includes different operators, control
structures, default variables for arguments, returning complex objects
vec1 <- c(0, 2); vec2 <- c(2, 3)
cat("Addition of vectors:", vec1 + vec2, "\n")
cat("Multiplication of vectors:", vec1 * vec2, "\n")
cat("Division of vectors:", vec1 / vec2, "\n")
cat("Modulo of vectors:", vec1 %% vec2, "\n")
cat("Power operator:", vec1 ^ vec2, "\n\n")
vec3 <- c(TRUE, FALSE)
cat("Element wise AND:", vec1 & vec3, "\n")
cat("Element wise OR:", vec1 | vec3, "\n")
cat("Logical AND:", vec1[1] && vec3[1], "\n")
cat("Logical OR:", vec1[1] || vec3[1], "\n")
cat("Negation:", !vec1, "\n\n")
mat <- matrix(1:4, nrow = 1)
cat('"matrix element using:"\n')
print(mat)
product <- mat %*% t(mat)
cat('"Product of matrices:"\n')
print(product)
cat("Does 1 exist in prod matrix:", 1 %in% product, "\n\n")
x <- 5
if (x < 10) {
cat('"5 is lesser than 10"\n\n')
}
for (i in letters[4]) cat('"', i, '"', sep = "")
4.Write a R program for quick shot implementation, binary search tree
createNode <- function(key) list(key = key, left = NULL, right = NULL)
insert <- function(root, key) {
if (is.null(root)) return(createNode(key))
if (key < root$key) root$left <- insert(root$left, key)
else root$right <- insert(root$right, key)
return(root)
}
inorder <- function(root) {
if (is.null(root)) return(c())
return(c(inorder(root$left), root$key, inorder(root$right)))
}
quicksort_bst <- function(arr) {
bst <- NULL
for (key in arr) bst <- insert(bst, key)
sorted_array <- inorder(bst)
cat("Sorted Array: ", sorted_array, "\n")
cat("In-order Traversal: ", sorted_array, "\n")
return(sorted_array)
}
unsorted_array <- c(10, 5, 2, 7, 15, 12, 20, 8)
sorted_array <- quicksort_bst(unsorted_array)
5.Write a R program for calculating cumulative sums, and products
minimam, maximam and calculus

my_vector <- c(2, 4, 6, 8, 10)


cumulative_sum <- cumsum(my_vector)
cat("Cumulative Sum:", cumulative_sum, "\n")
cumulative_product <- cumprod(my_vector)
cat("Cumulative Product:", cumulative_product, "\n")
min_value <- cummin(my_vector)
max_value <- cummax(my_vector)
cat("Cumulative Minimum Value:", min_value, "\n")
cat("Cumulative Maximum Value:", max_value, "\n")
derivative <- diff(my_vector)
cat("Derivative:", derivative, "\n")
integral <- cumsum(my_vector)
cat("Integral:", integral, "\n")
6.Write R program for finding stationery distribution of Markanovchains
library(markovchain)
transition_matrix <- matrix(c(0.7, 0.2, 0.1, 0.3, 0.6, 0.1, 0.2, 0.3, 0.5), nrow
= 3, byrow = TRUE)
cat(transition_matrix)
markov_chain <- new("markovchain", states = c("State1", "State2",
"State3"),
transitionMatrix = transition_matrix, name = "Example Markov Chain")
stationary_distribution <- steadyStates(markov_chain)
cat("Stationary Distribution:\n")
print(stationary_distribution)
7.Write a R program that include linear algebra operation on vectors
and matrices
v1 <- c(1, 2, 3)
v2 <- c(4, 5, 6)
cat("Vector 1:", v1, "\n")
cat("Vector 2:", v2, "\n")
addition_result <- v1 + v2
cat("Vector Addition Result:", addition_result, "\n")
subtraction_result <- v1 - v2
cat("Vector subtraction Result:", subtraction_result, "\n")
scalar <- 2
scalar_multiplication_result <- scalar * v1
cat("Scalar Multiplication Result:", scalar_multiplication_result, "\n")
dot_product_result <- sum(v1 * v2)
cat("Dot Product Result:", dot_product_result, "\n")
m1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE)
m2 <- matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, byrow = TRUE)
cat("Matrix 1:\n", m1, "\n")
cat("Matrix 2:\n", m2, "\n")
matrix_addition_result <- m1 + m2
cat("Matrix Addition Result:\n", matrix_addition_result, "\n")
matrix_subtaction_result <- m1 - m2
cat("Matrix subtraction Result:\n", matrix_subtaction_result, "\n")
matrix_multiplication_result <- m1 %*% t(m2)
cat("Matrix Multiplication Result:\n", matrix_multiplication_result, "\n")
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 rainfall <- c(7, 12, 28, 3, 41)
months <- c("Jan", "Feb", "Mar", "Apr", "May")
# Plot
plot(rainfall, col = "red", xlab = "Months", ylab = "Rainfall (mm)", main =
"Rainfall Chart")
plot(rainfall, type = "o", col = "red", xlab = "Months", ylab = "Rainfall (mm)",
main = "Rainfall Chart")
# Bar plot barplot(rainfall, main = "Rainfall Chart", xlab = "Months", ylab =
"Rainfall (mm)", names.arg = months)
# Pie chart
pie(rainfall, labels = rainfall, main = "Rainfall Chart", col =
rainbow(length(rainfall)))
legend("topright", months, cex = 0.8, fill = rainbow(length(rainfall)))
# Box plot
rainfall_data <- data.frame(Month = months, Rainfall = rainfall)
boxplot(Rainfall ~ Month, data = rainfall_data, main = "Rainfall Boxplot by
Month", xlab = "Month", ylab = "Rainfall (mm)", col = "skyblue", border =
"black")
# Histogram
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39)
hist(v, xlab = "No. of Articles", col = "green", border = "black", xlim = c(0, 50),
ylim = c(0, 5), breaks = 5)
# Scatter plot
input <- mtcars[, c('wt', 'mpg')]
plot(input$wt, input$mpg, xlab = "Weight", ylab = "Milage", xlim = c(1.5, 4),
ylim = c(10, 25), main = "Weight vs Milage")
9.Write a R program for with any dataset containing dataframe objects,
indexing and subsetting data frames, and employ manipulating and
analyzing data
data = data.frame(
Name = c("Sachin", "Smriti", "Sheyanka", "Virat", "Dipti"),
Age = c(25, 30, 22, 35, 28),
Gender = c("Male", "Female", "Female", "Male", "Female"),
Score = c(78, 85, 62, 92, 80)
)
print("Original Data:")
print(data)
subset_data <- data[data$Age > 25, ]
print("\nSubset Data (Age > 25):")
print(subset_data)
data$Grade <- ifelse(data$Score >= 80, "A", "B")
print("\nData with Grade column added:")
print(data)
summary(data)
10.Write a program to create an any application of Linear Regression in
multivariate context for predictive purpose.
print("Application of Linear Regression in multivariate context for predictive
purpose")
mtcars
input=mtcars[c("mpg","wt","cyl")]
x=input[1:5,]
a=lm(mpg~wt+cyl,x)
y=mtcars[c("wt","cyl")]
nd=y[6:10,]
nd
result=predict(a,nd)
cat("compare\n")
compare=data.frame(predicted=result,actual=mtcars$mpg[6:10])
compare

You might also like