21. Create a matrix and calculate the row-wise and column-wise sums.
Required Input:
Matrix:
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
Expected Output:
Row-wise sums: 3 7 11
Column-wise sums: 9 12
Code In R
# Predefined structure
matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, byrow = TRUE)
row_sums <- rowSums(matrix)
col_sums <- colSums(matrix)
print(paste("Row-wise sums:", paste(row_sums, collapse = " ")))
print(paste("Column-wise sums:", paste(col_sums, collapse = " ")))
Run Code?
Click Run Button to view compiled output
22. Write an R program to check if a given vector contains a specific element.
Required Input:
Vector: c(1, 2, 3, 4, 5)
Element: 3
Expected Output:
Element 3 is present in the vector
Code In R
# Predefined structure
vector <- c(1, 2, 3, 4, 5)
element <- 3
if (element %in% vector) {
print(paste("Element", element, "is present in the vector"))} else {
print(paste("Element", element, "is not present in the vector"))}
Run Code?
Click Run Button to view compiled output
23. Create a vector and calculate the standard deviation of its elements.
Required Input:
Vector: [10, 20, 30, 40, 50]
Expected Output:
Standard Deviation: 15.81
Code In R
# Predefined structure
vector <- c(10, 20, 30, 40, 50)
std_dev <- sd(vector)
print(paste("Standard Deviation:", round(std_dev, 2)))
Run Code?
Click Run Button to view compiled output
24. Write an R program to merge two data frames by a common column.
Required Input:
Data Frame 1:
ID Name
1 1 John
2 2 Alice
3 3 Bob
Data Frame 2:
ID Age
1 1 25
2 2 30
3 3 22
Expected Output:
ID Name Age
1 1 John 25
2 2 Alice 30
3 3 Bob 22
Code In R
# Predefined structure
df1 <- data.frame(ID = c(1, 2, 3), Name = c("John", "Alice", "Bob"))
df2 <- data.frame(ID = c(1, 2, 3), Age = c(25, 30, 22))
merged_df <- merge(df1, df2, by = "ID")
print(merged_df)
Run Code?
Click Run Button to view compiled output
25. Write an R program to replace all negative numbers in a vector with zeros.
Required Input:
Vector: c(-5, 3, -2, 7, -1)
Expected Output:
[1] 0 3 0 7 0
Code In R
# Predefined structure
vector <- c(-5, 3, -2, 7, -1)
vector[vector < 0] <- 0
print(vector)
Run Code?
Click Run Button to view compiled output
26. Create a named list with three elements: a vector, a matrix, and a data frame. Access one element by its name.
Required Input:
None
Expected Output:
$vector
[1] 1 2 3
Accessed element:
[1] 1 2 3
Code In R
# Predefined structure
named_list <- list(vector = c(1, 2, 3), matrix = matrix(1:4, nrow = 2), data_frame = data.frame(Name = c("Alice", "Bob"), Age = c(25, 30)))
print(named_list$vector)
Run Code?
Click Run Button to view compiled output
27. Write an R program to find the correlation between two numeric vectors.
Required Input:
Vector 1: c(1, 2, 3, 4, 5)
Vector 2: c(2, 4, 6, 8, 10)
Expected Output:
Correlation: 1
Code In R
# Predefined structure
vector1 <- c(1, 2, 3, 4, 5)
vector2 <- c(2, 4, 6, 8, 10)
correlation <- cor(vector1, vector2)
print(paste("Correlation:", correlation))
Run Code?
Click Run Button to view compiled output