0% found this document useful (0 votes)
3 views4 pages

1st Sem R Programming Lab

The document contains practical R programming exercises focusing on set operations, inverse functions, one-to-one functions, Cartesian products, and various types of relations (reflexive, transitive, symmetric). Each section includes code snippets and their corresponding outputs demonstrating the functionality. Additionally, it covers basic logic gates with examples.

Uploaded by

deepadevaraju21
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)
3 views4 pages

1st Sem R Programming Lab

The document contains practical R programming exercises focusing on set operations, inverse functions, one-to-one functions, Cartesian products, and various types of relations (reflexive, transitive, symmetric). Each section includes code snippets and their corresponding outputs demonstrating the functionality. Additionally, it covers basic logic gates with examples.

Uploaded by

deepadevaraju21
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

1st R Programming Practical Programs

1. Set Operations (Union, Intersection, Difference, Subset)

A <- c(1, 2, 3, 4)
B <- c(3, 4, 5, 6)

cat("Union: ", union(A, B), "\n")


cat("Intersection: ", intersect(A, B), "\n")
cat("Difference A - B: ", setdiff(A, B), "\n")
cat("Difference B - A: ", setdiff(B, A), "\n")
cat("Is A subset of B?: ", all(A %in% B), "\n")

Output:
Union: 1 2 3 4 5 6
Intersection: 3 4
Difference A - B: 1 2
Difference B - A: 5 6
Is A subset of B?: FALSE

2. Inverse Function

inverse_function <- function(f) {


inv <- setNames(names(f), f)
return(inv)
}

f <- c("a"="1", "b"="2", "c"="3")


inv <- inverse_function(f)
print(inv)

Output:
123
"a" "b" "c"

3. One-to-One Function

is_one_to_one <- function(f) {


length(unique(f)) == length(f)
}

f <- c("a"="1", "b"="2", "c"="3")


cat("Is one-to-one?:", is_one_to_one(f), "\n")

Output:
Is one-to-one?: TRUE

4. Cartesian Product

A <- c(1, 2)
B <- c("a", "b")

product <- [Link](A, B)


colnames(product) <- c("A", "B")
print(product)

Output:
AB
11a
22a
31b
42b

5. Reflexive Relation

is_reflexive <- function(R, set) {


all(sapply(set, function(x) any(sapply(R, function(p) all(p == c(x,x))))))
}

R <- list(c(1,1), c(2,2), c(3,3), c(1,2))


set <- c(1, 2, 3)
cat("Is Reflexive?:", is_reflexive(R, set), "\n")

Output:
Is Reflexive?: TRUE
6. Transitive Relation

is_transitive <- function(R) {


for (a in R) {
for (b in R) {
if (a[2] == b[1]) {
if (!any(sapply(R, function(x) all(x == c(a[1], b[2]))))) {
return(FALSE)
}
}
}
}
return(TRUE)
}

R <- list(c(1,2), c(2,3), c(1,3))


cat("Is Transitive?:", is_transitive(R), "\n")

Output:
Is Transitive?: TRUE

7. Logic Gates (NOT, AND, OR, XOR)

a <- TRUE
b <- FALSE

cat("NOT a:", !a, "\n")


cat("a AND b:", a & b, "\n")
cat("a OR b:", a | b, "\n")
cat("a XOR b:", xor(a, b), "\n")

Output:
NOT a: FALSE
a AND b: FALSE
a OR b: TRUE
a XOR b: TRUE

8. Symmetric Relation

is_symmetric <- function(R) {


for (pair in R) {
if (!any(sapply(R, function(x) all(x == rev(pair))))) {
return(FALSE)
}
}
return(TRUE)
}

R <- list(c(1,2), c(2,1), c(3,3))


cat("Is Symmetric?:", is_symmetric(R), "\n")

Output:
Is Symmetric?: TRUE

You might also like