0% found this document useful (0 votes)
15 views3 pages

SCIBD

SCRIBBBBBBB

Uploaded by

yashsingla160405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

SCIBD

SCRIBBBBBBB

Uploaded by

yashsingla160405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# 1. Create a numeric vector with the values 1 to 10, access the 3rd element.

vec <- 1:10


third_element <- vec[3]
third_element

# 2. Perform element-wise addition between two vectors.


v1 <- c(2, 4, 6)
v2 <- c(1, 3, 5)
sum_vectors <- v1 + v2
sum_vectors

# 3. Find the mean and sum of a numeric vector.


v <- c(10, 20, 30, 40)
mean_v <- mean(v)
sum_v <- sum(v)
mean_v
sum_v

# 4. Create a character vector and append "Date" to it.


fruits <- c("Apple", "Banana", "Cherry")
fruits <- c(fruits, "Date")
fruits

# 5. Check if the vector contains the value 5.


x <- c(2, 4, 6, 8)
contains_five <- 5 %in% x
contains_five

# 6. Create a 3x3 matrix filled row-wise.


matrix_3x3 <- matrix(1:9, nrow = 3, byrow = TRUE)
matrix_3x3

# 7. Multiply two matrices.


A <- matrix(1:4, nrow = 2)
B <- matrix(5:8, nrow = 2)
matrix_product <- A %*% B
matrix_product

# 8. Extract element from 2nd row and 3rd column of a 3x4 matrix.
matrix_3x4 <- matrix(1:12, nrow = 3, ncol = 4)
element_2_3 <- matrix_3x4[2, 3]
element_2_3

# 9. Compute the transpose of a matrix.


M <- matrix(1:6, nrow = 2, ncol = 3)
transpose_M <- t(M)
transpose_M

# 10. Perform element-wise multiplication of two matrices.


M1 <- matrix(1:4, nrow = 2)
M2 <- matrix(5:8, nrow = 2)
elementwise_product <- M1 * M2
elementwise_product

# 11. Create a 3D array with 2 matrices of size 2x2 using values 1 to 8.


array_3D <- array(1:8, dim = c(2, 2, 2))
array_3D

# 12. Access element in the 1st row, 2nd column, and 2nd matrix of the array.
element_3D <- array_3D[1, 2, 2]
element_3D

# 13. Add two 3D arrays of size 2x2x2.


A <- array(1:8, dim = c(2, 2, 2))
B <- array(9:16, dim = c(2, 2, 2))
sum_3D_arrays <- A + B
sum_3D_arrays

# 14. Find the sum of all elements in a 3D array.


sum_elements_3D <- sum(array_3D)
sum_elements_3D

# 15. Create a factor and check its levels.


f <- factor(c("Low", "High", "Medium", "High", "Low"))
levels(f)

# 16. Change the levels of a factor.


levels(f) <- c("L", "H", "M")
f

# 17. Sort a factor by its levels.


f_sorted <- factor(c("Small", "Medium", "Large"), levels = c("Small", "Medium",
"Large"))
f_sorted

# 18. Count the frequency of each level in a factor.


f_count <- factor(c("Yes", "No", "Yes", "No", "Yes"))
table(f_count)

# 19: Convert a factor f = factor(c("Low", "Medium", "High")) into numeric


f <- factor(c("Low", "Medium", "High"))
numeric_f <- as.numeric(f)

# 20: Remove the data frame element from the list


# Assuming you have a list named my_list containing a data frame named df
my_list <- list(df = data.frame(Name = c("Alice", "Bob"), Age = c(30, 22), Gender =
c("F", "M")))
my_list$df <- NULL # Remove the data frame element from the list

# 21: Create a data frame with columns Name, Age, and Gender
df <- data.frame(
Name = c("Ram", "Sham", "Yash"),
Age = c(40, 12, 39),
Gender = c("F", "M", "M")
)

# 22: Add a new column Country = c("USA", "UK", "Canada") to the existing data
frame df
df$Country <- c("USA", "UK", "Canada")

# 23: Filter rows in the data frame where Age > 25


filtered_df <- df[df$Age > 25, ]

# 24: Select only the Name and Age columns from the data frame
name_age_df <- df[, c("Name", "Age")]

# 25: Calculate the average (mean) of the Age column in the data frame
average_age <- mean(df$Age)
yyy

print(numeric_f)
print(my_list)
print(df)
print(filtered_df)
print(name_age_df)
print(average_age)

You might also like