Write A R Program For Different Types of Data Structures
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