Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

1. Write an R program Illustrate with if-else statement and how does it operate on vectors of variable length. || R

# Create a vector of numbers with variable length 

numbers <- c(2, 7, 10, 15, 8, 3, 6) 

# Initialize an empty vector to store the results 

results <- vector() 

# Use a for loop to iterate through the numbers 

for (num in numbers) { 

# Check if the number is even or odd 

if (num %% 2 == 0) { 

result <- "Even" 

} else { 

result <- "Odd" 

# Append the result to the results vector 

results <- c(results, result) 

# Print the original vector and the results 

cat("Original Vector: ", numbers, "\n") 

cat("Classification: ", results, "\n")

Post a Comment

0 Comments