30 R Programming Basic Exercises for Beginners with Solutions
Master beginner 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 intermediate challenges. Start your journey to R Programming mastery today!
Learning Objectives:
Understand the basics of R syntax, data types, vectors, and control structures. Develop foundational skills for data manipulation, plotting, and simple statistical analysis.
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 print "Hello, World!".
Required Input:
None
Expected Output:
Hello, World!
Code In R
Run Code?
Click Run Button to view compiled output
2. Create a variable x with the value 10 and print it.
Required Input:
None
Expected Output:
10
Code In R
Run Code?
Click Run Button to view compiled output
3. Write an R program to find the sum of two numbers.
Required Input:
Number 1: 5
Number 2: 15
Expected Output:
Sum: 20
Code In R
Run Code?
Click Run Button to view compiled output
4. Create a sequence of numbers from 1 to 10 using a built-in function.
Required Input:
None
Expected Output:
1 2 3 4 5 6 7 8 9 10
Code In R
Run Code?
Click Run Button to view compiled output
5. Write an R program to check if a number is even or odd.
Required Input:
Number: 7
Expected Output:
7 is odd
Code In R
Run Code?
Click Run Button to view compiled output
6. Create a vector containing the numbers 1, 2, 3, 4, 5 and print it.
Required Input:
None
Expected Output:
[1] 1 2 3 4 5
Code In R
Run Code?
Click Run Button to view compiled output
7. Write an R program to calculate the square of each number in a vector.
Required Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
[1] 1 4 9 16 25
Code In R
Run Code?
Click Run Button to view compiled output
8. Create a matrix with 2 rows and 3 columns and fill it with numbers from 1 to 6.
Required Input:
None
Expected Output:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Code In R
Run Code?
Click Run Button to view compiled output
9. Write an R program to find the mean of a given vector.
Required Input:
Vector: [10, 20, 30, 40, 50]
Expected Output:
Mean: 30
Code In R
Run Code?
Click Run Button to view compiled output
10. Write an R program to calculate the sum of all elements in a vector.
Required Input:
Vector: [5, 10, 15, 20, 25]
Expected Output:
Sum: 75
Code In R
Run Code?
Click Run Button to view compiled output