11. Create a function that accepts a numeric vector and returns only the even numbers.
Required Input:
Vector: c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Expected Output:
[1] 2 4 6 8 10
Code In R
# Predefined structure
filter_even <- function(vector) {
# Your logic here
}
result <- filter_even(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
print(result)
Run Code?
Click Run Button to view compiled output
12. Write a function to reverse the rows of a data frame.
Required Input:
Data Frame:
Name Age
1 John 25
2 Alice 30
3 Bob 22
Expected Output:
Name Age
3 Bob 22
2 Alice 30
1 John 25
Code In R
# Predefined structure
reverse_rows <- function(df) {
# Your logic here
}
reversed_data <- reverse_rows(data)
print(reversed_data)
Run Code?
Click Run Button to view compiled output
13. Create a list of 5 named elements, access each element, and print its type.
Required Input:
List: Name: "John"
Age: 30
Scores: c(85, 90, 75)
Passed: TRUE
Address: "123 Main St"
Expected Output:
[1] "John"
[1] "Type: character"
[1] 30
[1] "Type: numeric"
[1] 85 90 75
[1] "Type: numeric"
[1] TRUE
[1] "Type: logical"
[1] "123 Main St"
[1] "Type: character"
Code In R
# Predefined structure
my_list <- # Create the list
for (item in names(my_list)) {
print(my_list[[item]])
print(paste("Type:", class(my_list[[item]])))
}
Run Code?
Click Run Button to view compiled output
14. Write an R program to perform basic arithmetic operations (+, -, *, /) on two numeric vectors.
Required Input:
Vector 1: c(2, 4, 6)
Vector 2: c(1, 2, 3)
Expected Output:
Addition: 3 6 9
Subtraction: 1 2 3
Multiplication: 2 8 18
Division: 2 2 2
Code In R
# Predefined structure
vector1 <- # Create the first vector
vector2 <- # Create the second vector
addition <- vector1 + vector2
subtraction <- vector1 - vector2
multiplication <- vector1 * vector2
division <- vector1 / vector2
print(paste("Addition:", addition))
print(paste("Subtraction:", subtraction))
print(paste("Multiplication:", multiplication))
print(paste("Division:", division))
Run Code?
Click Run Button to view compiled output
15. Create a function to normalize the values in a numeric vector between 0 and 1.
Required Input:
Vector: c(10, 20, 30, 40, 50)
Expected Output:
Normalized: 0 0.25 0.5 0.75 1
Code In R
# Predefined structure
normalize <- function(vector) {
# Your logic here
}
normalized_vector <- normalize(vector)
print(paste("Normalized:", round(normalized_vector, 2)))
Run Code?
Click Run Button to view compiled output
16. Write a function to replace NA values in a vector with the median of the non-NA values.
Required Input:
Vector: c(10, 20, NA, 40, 50)
Expected Output:
Replaced Vector: 10 20 30 40 50
Code In R
# Predefined structure
replace_na_with_median <- function(vector) {
# Your logic here
}
result <- replace_na_with_median(vector)
print(paste("Replaced Vector:", result))
Run Code?
Click Run Button to view compiled output
17. Write a program to calculate the dot product of two vectors.
Required Input:
Vector 1: c(1, 2, 3)
Vector 2: c(4, 5, 6)
Expected Output:
Dot Product: 32
Code In R
# Predefined structure
vector1 <- # Define the first vector
vector2 <- # Define the second vector
dot_product <- # Calculate the dot product
print(paste("Dot Product:", dot_product))
Run Code?
Click Run Button to view compiled output
18. Write a function to compute the mode of a numeric vector.
Required Input:
Vector: c(1, 2, 2, 3, 3, 3, 4, 4, 5)
Expected Output:
Mode: 3
Code In R
# Predefined structure
find_mode <- function(vector) {
# Your logic here
}
result <- find_mode(vector)
print(paste("Mode:", result))
Run Code?
Click Run Button to view compiled output
19. Write an R program to perform linear regression and predict values based on a numeric data frame.
Required Input:
Data Frame:
x y
1 1 2
2 2 4
3 3 6
4 4 8
Expected Output:
Predicted Value for x=5: 10
Code In R
# Predefined structure
data <- # Create the data frame
model <- # Fit a linear regression model
prediction <- predict(model, data.frame(x = 5))
print(paste("Predicted Value for x=5:", prediction))
Run Code?
Click Run Button to view compiled output
20. Write a program to calculate the eigenvalues and eigenvectors of a given matrix.
Required Input:
Matrix:
[,1] [,2]
[1,] 4 2
[2,] 1 3
Expected Output:
[1] "Eigenvalues:"
[1] 5 2
[1] "Eigenvectors:"
[,1] [,2]
[1,] 0.7071068 -0.4472136
[2,] 0.7071068 0.8944272
Code In R
# Predefined structure
matrix <- # Create the matrix
eigen_result <- # Calculate eigenvalues and eigenvectors
print(eigen_result)
Run Code?
Click Run Button to view compiled output