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:
NoneExpected Output:
[1] 0 1 1 2 3 5 8 13 21 34
Code In R
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: 4Expected Output:
Result: 16Code In R
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 > 25Expected Output:
Name Age
2 Alice 30
Code In R
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 15Code In R
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
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: 20Expected Output:
[1] 3 5 7 11 13 17 19
Code In R
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.81Code In R
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 palindromeCode In R
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: AgeExpected Output:
Mean of Age: 25.67Code In R
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 8Expected Output:
[,1] [,2]
[1,] 5 21
[2,] 12 32
Code In R
Run Code?
Click Run Button to view compiled output

