lab_taskR
lab_taskR
#3.Write R code for the following problem: Create a 2x3 matrix M using R
programming with following specications. Values of the matrix should be randomly
selected between 10 and 30. Elements of the matrix should be arranged in row order.
# Set seed for reproducibility
set.seed(123)
# (a) Create a 2x3 matrix M with values randomly selected between 10 and 30
M <- matrix(sample(10:30, 2*3, replace = TRUE), nrow = 2, byrow = TRUE)
rownames(M) <- c("row1", "row2")
colnames(M) <- c("col1", "col2", "col3")
print("Matrix M:")
print(M)
M <- rbind(M, V)
print("Matrix M with vector V added as the third row:")
print(M)
# (b) Access the elements of the matrix using row names and column names
print("Accessing elements using row and column names:")
print(paste("Element at row1, col2:", M["row1", "col2"]))
print(paste("Element at row2, col3:", M["row2", "col3"]))
#4). convert ”is is a fun sentence” to ”is is a great sentence” using gsub in R.
original_string <- "is is a fun sentence"
modified_string <- gsub("fun", "great", original_string)
print(modified_string)
sum_series <- 0
current_term <- 1
for (i in 1:num_terms) {
sum_series <- sum_series + current_term
current_term <- current_term * 10 + 1
}
return(sum_series)
}
#12). Write a R Program to print all the perfect numbers in a given Range using
functions.
is_perfect <- function(num) {
sum_of_divisors <- sum(which(num %% 1:(num-1) == 0))
return(sum_of_divisors == num)
}
#14. For a given vector x < −c(1, 2, 2, 5, 5, 6) write R command to drop duplicate
elements.
x <- c(1, 2, 2, 5, 5, 6)
unique_elements <- unique(x)
print(unique_elements)
#15. For a given vector y < −c(1, 2, 3, NA) write R command to and the missing
valuesin Y.
y <- c(1, 2, 3, NA)
missing_values <- y[is.na(y)]
print(missing_values)
#16. for a given vector z < −c(15, 20, 32, 45, 59) write R command to recode the
values less than 32 in Z with 99 and the value 32 with NA.
z <- c(15, 20, 32, 45, 59)
z[z < 32] <- 99
z[z == 32] <- NA
print(z)
#17. Write an R program to create a 4 by 4 integer matrix and print the elements in
a matrix that are divisible by 3.
matrix <- matrix(c(1:16), nrow = 4, byrow = TRUE)
cat("Original Matrix:\n")
print(matrix)
#19. Write R program to add zeros at starting and ending of a numeric vector
num <- c(1, 2, 3, 4, 5)
paste0(0,num,0)
#24. write R code for the given problem Assuming students having roll numbers in a
particular class from 1 to 60.The roll numbers of students who are absent for a
particular class on wednesday are
1,2,8,9,11,13,14,17,20,22,24,25,26,29,32,36,45,47,49,50,53,54,58. print the roll
numbers who are present
absent_roll_numbers <- c(1, 2, 8, 9, 11, 13, 14, 17, 20, 22, 24, 25, 26, 29, 32,
36, 45, 47, 49, 50, 53, 54, 58)
all_roll_numbers <- 1:60
present_roll_numbers <- all_roll_numbers[!all_roll_numbers %in%
absent_roll_numbers]
print(present_roll_numbers)
print(prime_numbers)
#27. consider the following Data x < −c(2, 2, 6, 7, 8, 6) write R commands for the
following.
x <- c(2, 2, 6, 7, 8, 6)
#(b) create a vector with 10 values and sort them in ascending and descending
order.
vector <- c(5, 2, 9, 3, 7, 1, 8, 4, 10, 6)
ascend <- sort(vector)
descend <- sort(vector, decreasing = TRUE)
print("Sorted in ascending order:")
print(ascend)
print("Sorted in descending order:")
print(descend)
#29. consider the following vector x < −c(10, 11, 12, NA, 14, 15, 16, NA, 18, NA,
19) Write R commands for the following
# Given vector
x <- c(10, 11, 12, NA, 14, 15, 16, NA, 18, NA, 19)
# (a) Find the size of a vector and find whether it is a vector or not
vector_size <- length(x)
is_vector <- is.vector(x)
# (c) Replace all the missing values with Mean of remaining values and print the
updated vector
x_no_na <- x[!is.na(x)] # Extract non-NA values
mean_value <- mean(x_no_na, na.rm = TRUE)
x_updated <- ifelse(is.na(x), mean_value, x)
print("Updated vector:")
print(x_updated)