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

# Predefined structure print("Hello, World!")

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

# Predefined structure x <- 10 print(x)

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

# Predefined structure num1 <- 5 num2 <- 15 sum <- num1 + num2 print(paste("Sum:", sum))

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

# Predefined structure sequence <- seq(1, 10) print(sequence)

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

# Predefined structure num <- 7 if (num %% 2 == 0) { print(paste(num, "is even")) } else { print(paste(num, "is odd"))}

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

# Predefined structure vector <- c(1, 2, 3, 4, 5) print(vector)

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

# Predefined structure vector <- c(1, 2, 3, 4, 5) squared <- vector^2 print(squared)

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

# Predefined structure matrix <- matrix(1:6, nrow = 2, byrow = FALSE) print(matrix)

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

# Predefined structure vector <- c(10, 20, 30, 40, 50) mean_value <- mean(vector) print(paste("Mean:", mean_value))

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

# Predefined structure vector <- c(5, 10, 15, 20, 25) sum_value <- sum(vector) print(paste("Sum:", sum_value))

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3