Open In App

R Programming Exercises, Practice Questions and Solutions

Improve
Improve
Like Article
Like
Save
Share
Report

R Programming Language is an open-source language mostly used for machine learning, statistics, data visualization, etc. R was developed by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand. R is similar to S programming language which is a GNU project created by John Chambers and his team at Bell Laboratories.

It comes with a command-line interface and provides a vast list of packages for performing tasks. It is an interpreted language that supports both object-oriented and procedural programming and it is available on widely used platforms e.g. Windows, Linux and Mac. You might have seen various R tutorials explaining the concepts and the theoretical part with some examples, but that is not enough to understand this language. You need more practice to make yourself perfect as practice will make you perfect.

R Programming Exercises, Practice Questions and Solutions

This R Programming Exercise article will cover all R programming practice Questions and learn R Language. You can sharpen your R programming Skills using sets of questions from basic to advance, containing a well-explained and detailed solution to each question.

Table of Content

  • Basics – R Programming (14 exercises with solution)
  • Lists – R Programming
  • Data Types – R Programming
  • Strings – R Programming
  • Functions – R Programming
  • Loops – R Programming
  • If Else – R programming
  • Variable – R programming
  • Vector – R Programming
  • Matrix – R Programming
  • DataFrame – R Programming
  • Factor – R Programming
  • Data and Time – R Programming
  • CSV – R Programming
  • Excel – R Programming

List of R Exercises with Solutions :

R Programming Language – Basic Exercises with Solution

  1. Write an R Program for “Hello Geeks”.Write an R Program to Add Two Vectors
  2. Find the Sum, Mean and Product of the Vector in R Programming
  3. Create an R Program to Take Input From the User
  4. How to Generate Random Numbers from Standard Distributions in R
  5. R Program to Sample from a Population
  6. Create an R Program to Find the Minimum and Maximum
  7. R Program to Sort a Vector
  8. How to Find the Factorial of a Number
  9. How to create R Multiplication Table
  10. Write an R Program to Check Prime Number
  11. R Program to check Armstrong Number
  12. R Program to Print the Fibonacci Sequence
  13. R Program to Check for Leap Year
  14. Check if a Number is Odd or Even in R Programming

R Programming Language – List Exercises with Solution

  1. Count the Number of List Elements in R
  2. Create a list with random values in R
  3. How to add Key Value Pair to List in R?
  4. Access Index Names of List Using apply Function in R
  5. Convert matrix to list in R
  6. Convert the list to a data frame with specific column names in R
  7. Convert list to array in R

Also, check: More Programs on Lists

R Programming Language – Data Types Exercises with Solution

  1. R Data Types
  2. Data Type Conversion in R
  3. Getting different data types in R Programming – a type of the() Function.

R Programming Language – String Exercises with Solution

  1. Convert Character String to Variable Name in R
  2. Count the Number of Characters in the String in R
  3. Count Number of Occurrences of Certain Character in String in R
  4. Extract Numbers from the Character String Vector in R
  5. Count the Number of Words in a String using R
  6. How to calculate the number of occurrences of a character in each row of the R data frame?
  7. Write a Program to Concatenate Two Strings in R.
  8. R Program to Find the Length of a String
  9. How to Check if Characters are Present in a String in R.
  10. R Program to Extract n Characters From a String
  11. How to Replace Characters in a String in R
  12. Create a Program to Compare Two Strings in R.
  13. R Program to Convert Factors to Characters
  14. R Program to Trim Leading and Trailing Whitespaces

Also, check: More Programs on Strings

R Programming Language – Functions Exercises with Solution

  1. Types of Functions in R Programming
  2. Function Arguments in R Programming

R Programming Language – Looping Exercises with Solution

  • For loop in R
    1. for loop to print the elements of a vector?
    2. The sum of parts in a vector using a for loop?
    3. Finding the maximum value in a vector using a for loop?
    4. Reversing a vector using a for loop?
    5. Counting the number of even and odd elements in a vector using a for loop?
  • R – while loop
    1. while loop to print the elements of a vector?
    2. while loop to find the first occurrence of a specific element in a vector?
    3. while loop to calculate the factorial of a number?
    4. while loop to calculate the square of numbers?
    5. while loop to reverse a string?
  • Looping over Objects in R Programming
  • R – Repeat the loop
    1. repeat to print the elements of a vector.
    2. Repeat loop to generate random numbers until a number greater than 0.9 is generated?
    3. repeat loop to generate a sequence of numbers?
  • How to Create a Nested For Loop in R?
    1. Nested for loop to print multiplication tables up to a certain number.
    2. Nested for loop to create a 2D matrix.
    3. Nested for loop to print a pattern.
    4. Nested for loop to calculate the transpose of a matrix.

R Programming Language – If … Else Exercises with Solution

  • R If Else Conditions
    1. Check if a number is positive or negative using if-else a statement.
    2. if-else to find the maximum of two numbers.
    3. Create a programme to assign grades based on a student’s score using if-else .
    4. Create a programme to categorize numbers into odd or even.
    5. if-else to check if a number is divisible by another number.
    6. if-else to categorize ages into different groups.
    7. if-else to check if a character string contains a specific substring.
  • Nested if-else statement in R
    1. Grade Classification Based on Multiple Conditions.
    2. Nested if-else for Temperature Classification.
    3. Quadrant Classification for Coordinates.

R Programming Language – Variable Exercises with Solution

  1. R Variables
  2. Scope of The Variables
  3. How to Create Categorical Variables in R?
  4. Accessing variables of a data frame in R Programming – attach() and detach() function
  5. Select variables (columns) in R using Dplyr
  6. Dummy Variables in R Programming

R Programming Language – Vector Exercises with Solution

  1. How to create an empty vector in R?
  2. Create empty vector and append values
  3. Find the Sum, Mean and Product of a Vector in R
  4. Find the product of vector elements in R
  5. Count the number of vector values in the range with R
  6. Count the specific value in a given vector in R
  7. Access the last value of a given vector in R
  8. Find the elements of a vector that are not in another vector in R
  9. Find the Nth highest value of a vector in R
  10. How to find Nth smallest value in vector in R?
  11. Extract every Nth element of a vector in R
  12. R Program to Concatenate a Vector of Strings
  13. How to Check if a Vector Contains the Given Element
  14. Write an R Program to Count the Number of Elements in a Vector
  15. R Program to Find Index of an Element in a Vector
  16. Write an R Program to Access Values in a Vector
  17. R Program to Add Leading Zeros to Vector

Also, check: More Programs on Vectors

R Programming Language – Matrix Exercises with Solution

  1. How to create an empty matrix in R?
  2. Fill an empty matrix in R
  3. Elementwise Matrix Multiplication in R
  4. Multiply Matrix by Vector in R
  5. Find the power of a matrix in R
  6. Raise a matrix to a fractional power in R
  7. Get the element at the specific position from the matrix in R
  8. Find the row and column index of maximum and minimum value in a matrix in R
  9. Select rows of a matrix in R that meet a condition
  10. Multiply a matrix by its transpose while ignoring missing values in R

Also, check: More Programs on Matrices

R Programming Language – DataFrame Exercises with Solution

  1. How to Convert a List to a Dataframe
  2. R Program to Create an Empty Dataframe
  3. How to Combine Two Dataframe into One
  4. Create an R Program to Change the Column Name of a Dataframe
  5. How to Extract Columns From a Dataframe
  6. R Program to Drop Columns in a Dataframe
  7. R Program to Reorder Columns in a Dataframe
  8. How to Split Dataframe
  9. R Program to Merge Multiple Dataframes
  10. R Program to Delete Rows From Dataframe
  11. R Program to Make a List of Dataframes
  12. How to create a data frame from given vectors in R?
  13. Create an empty DataFrame with only column names in R
  14. Insert multiple rows in R DataFrame
  15. How to add a column to the data frame in R?
  16. Extract the first N rows from the data frame in R
  17. How to select the row with the maximum value in each group in R Language?
  18. Remove rows with NA in one column of the R DataFrame
  19. How to remove empty rows from the R data frame?
  20. Find columns and rows with NA in R DataFrame
  21. Sort DataFrame by column name in R
  22. How To Merge Two DataFrames in R?
  23. Append one data frame to the end of another data frame in R
  24. How to find common rows and columns between two data frames in R?

Also, check: More Programs on DataFrame

R Programming Language – Factor Exercises with Solution

  1. How to count values per level in a factor in R
  2. Find the levels of a factor of a given vector in R
  3. How to change the order of levels of a factor in R?
  4. How to convert factor levels to list in R?
  5. Concatenate two given factors in a single factor in R
  6. Get All Factor Levels of the DataFrame Column in R

Also. check: More Programs on Factors

R Programming Language – Date and Time Exercises with Solution

  1. How to Add and Subtract Days to and from Date in R?
  2. How to subtract time in R?
  3. How to Extract time from the timestamp in R?
  4. How to calculate the number of days between two dates in R?
  5. How to calculate Years between Dates in R?
  6. How to convert a factor into a date format?

Also, check: More Programs on Date and Time

R Programming Language – File Handling Exercises with Solution

  1. How to check if a file already exists in R?
  2. R – Check if a Directory Exists and Create if It does not
  3. Add New Line to Text File in R
  4. How To Import Data from a File in R Programming
  5. How to export dataframe to RDATA file in R ?

Also, check: More Programs on File Handling

R Programming Language – CSV Exercises with Solution

  1. Reading the CSV file into Dataframes in R
  2. Export CSV File without Row Names in R
  3. How to write to CSV in R without index?
  4. Append row to CSV using R
  5. How to calculate the mean of a CSV file in R?

Also, check: More Programs on CSV

R Programming Language – Excel Exercises with Solution

  1. How to import an Excel File into R?
  2. How to export a DataFrame to Excel File in R?
  3. Convert an Excel column into a list of vectors in R
  4. How to convert an Excel column to a vector in R?
  5. How to convert Excel content into DataFrame in R?
  6. Delete rows with empty cells from Excel using R

Also, check: More Programs on Excel

R Programming Language – Data Visualization Exercises with Solution

  1. Adding Colors to Charts in R Programming
  2. How to show legend in heatmap in R?
  3. Display All X-Axis Labels of Barplot in R
  4. How to Create a Stacked Dot Plot in R?
  5. Change Spacing of Axis Tick Marks in Base R Plot
  6. Add legends without borders and with white backgrounds in R
  7. Plot Shaded Area between vertical lines in R
  8. How to add the Mean and Median to Histogram in R?
  9. Create a Scatter plot from CSV in R
  10. Customizing Colors in Plots.
  11. Adding Legends to Plots
  12. Creating Interactive Plots using Shiny
  13. Creating Interactive Plots using Shiny
  14. Annotating Text and Labels in Plots
  15. Formatting Axis Labels and Ticks in Plots
  16. Working with Multiple Plots (Faceting)
  17. Plotting Time Series Data in R
  18. Visualizing Geographic Data with Maps
  19. Creating Animated Plots in R
  20. Creating 3D Plots in R
  21. Working with Plotly for Interactive Visualizations
  22. Creating Trellis (Lattice) Plots in R
  23. Plotting Large Datasets with ggplot2’s geom_point() and geom_bin2d()
  24. Visualizing Hierarchical Data with Dendrograms
  25. Creating Sunburst Charts for Hierarchical Data
  26. Working with Word Clouds in R
  27. Network Visualization in R using graph
  28. Creating Heatmaps with Hierarchical Clustering
  29. Plotting Multiple Data Series in a Single Plot
  30. Interactive Data Visualization with Plotly Express

Conclusion

In Conclusion, R programming exercises are a complete guide for practising R programming Exercise Questions. After theoritical reading, The best way to master anything is by practice and exercise questions. Here you have the opportunity to practice the R programming language concepts by solving the exercises starting from basic to more complex exercises. A sample solution is provided for each exercise. It is recommended to do these exercises by yourself first before checking the solution. we hope, these exercises help you to improve your R programming coding skills. At present, the following sections are accessible, and we are diligently striving to incorporate additional exercises. Keep coding with enthusiasm!

R Programming Exercises – FAQs

1. What is the R programming language used for?

R is a language for statistical computing and graphics, commonly used in data analysis and visualization.

2. Is R free to use?

Yes, R is open-source and freely available for anyone to use, modify, and distribute.

3. Can I create plots and charts in R?

Yes, R has powerful libraries like ggplot2 that allow you to create a wide variety of visualizations easily.

4. What are packages in R?

Packages are collections of R functions and data, designed to extend the capabilities of R and make specific tasks easier.

5. How can I install packages in R?

You can install packages using the install.packages(“package_name”) command in the R console.



Last Updated : 11 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads