30 R Programming Basic Exercises for Intermediate with Solutions

Master intermediate R Programming skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in R Programming, setting a solid foundation for advanced challenges. Start your journey to R Programming mastery today!

Learning Objectives:

Enhance your ability to work with data frames, apply functions, and handle data wrangling using packages like dplyr and ggplot2. Strengthen your understanding of loops, apply family functions, and basic statistical modeling.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Write an R program to calculate the Fibonacci sequence up to the 10th term using a loop.

Required Input:

None

Expected Output:

 [1]  0  1  1  2  3  5  8 13 21 34

Code In R

# Predefined structure fibonacci <- function(n) { # Your logic here } print(fibonacci(10))

Run Code?

Click Run Button to view compiled output

2. Create a custom function in R to calculate the nth power of a number.

Required Input:

Base: 2
Exponent: 4

Expected Output:

Result: 16

Code In R

# Predefined structure power <- function(base, exponent) { # Your logic here } result <- power(2, 4) print(paste("Result:", result))

Run Code?

Click Run Button to view compiled output

3. Write an R program to filter rows in a data frame based on a specific condition.

Required Input:

Data Frame: 
 Name Age
1 John 25
2 Alice 30
3 Bob 22
Condition: Age > 25

Expected Output:

   Name Age
2 Alice  30

Code In R

# Predefined structure data <- data.frame(Name = c("John", "Alice", "Bob"), Age = c(25, 30, 22)) filtered_data <- data[data$Age > 25, ] print(filtered_data)

Run Code?

Click Run Button to view compiled output

4. Write a program to calculate the cumulative sum of a numeric vector.

Required Input:

Vector: [1, 2, 3, 4, 5]

Expected Output:

Cumulative Sum: 1 3 6 10 15

Code In R

# Predefined structure vector <- # Create the vector cumsum_result <- # Calculate the cumulative sum print("Cumulative Sum:") print(cumsum_result)

Run Code?

Click Run Button to view compiled output

5. Create a data frame and write a function to add a new column to it.

Required Input:

Data Frame: 
 Name Age
1 John 25
2 Alice 30
3 Bob 22
New Column: Salary = c(50000, 60000, 45000)

Expected Output:

   Name Age Salary
1  John  25  50000
2 Alice  30  60000
3   Bob  22  45000

Code In R

# Predefined structure data <- # Create the data frame add_column <- function(df, new_col_name, new_col_values) { df[[new_col_name]] <- new_col_values return(df) } updated_data <- add_column(data, "Salary", c(50000, 60000, 45000)) print(updated_data)

Run Code?

Click Run Button to view compiled output

6. Write an R program to find all the prime numbers less than a given number.

Required Input:

Number: 20

Expected Output:

[1]  3  5  7 11 13 17 19

Code In R

# Predefined structure find_primes <- function(n) { # Your logic here } print(find_primes(20))

Run Code?

Click Run Button to view compiled output

7. Write a program to calculate the variance and standard deviation of a numeric vector.

Required Input:

Vector: c(10, 20, 30, 40, 50)

Expected Output:

Variance: 250
Standard Deviation: 15.81

Code In R

# Predefined structure vector <- # Create the vector variance <- # Calculate variance std_dev <- # Calculate standard deviation print(paste("Variance:", variance)) print(paste("Standard Deviation:", round(std_dev, 2)))

Run Code?

Click Run Button to view compiled output

8. Write a function to check if a given string is a palindrome.

Required Input:

String: "madam"

Expected Output:

madam is a palindrome

Code In R

# Predefined structure is_palindrome <- function(string) { # Your logic here } is_palindrome("madam")

Run Code?

Click Run Button to view compiled output

9. Create a function that accepts a data frame and a column name and calculates the mean of that column.

Required Input:

Data Frame: 
 Name Age
1 John 25
2 Alice 30
3 Bob 22
Column: Age

Expected Output:

Mean of Age: 25.67

Code In R

# Predefined structure calculate_column_mean <- function(df, column_name) { # Your logic here } mean_age <- calculate_column_mean(data, "Age") print(paste("Mean of Age:", round(mean_age, 2)))

Run Code?

Click Run Button to view compiled output

10. Write a program to calculate the element-wise product of two matrices.

Required Input:

Matrix 1: 
 [,1] [,2]
[1,] 1 2
[2,] 3 4
Matrix 2: 
 [,1] [,2]
[1,] 5 6
[2,] 7 8

Expected Output:

     [,1] [,2]
[1,]    5   21
[2,]   12   32

Code In R

# Predefined structure matrix1 <- # Create the first matrix matrix2 <- # Create the second matrix product <- # Perform element-wise multiplication print(product)

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 2