Software Training Institute in Chennai with 100% Placements – SLA Institute
Share on your Social Media

R Programming Challenges and Solutions

Published On: September 29, 2025

R Programming Challenges and Solutions for Beginners

Master the basics of R with these real-world coding exercises! ???? This tutorial presents a carefully selected collection of problems to help you practice fundamental data manipulation, visualization, and statistics. Every challenge offers a chance to implement your knowledge in a practical manner, developing skills to perform data analysis and research.

Ready to take your data game to the next level? Check out our detailed R programming course syllabus to learn how you can become a skilled data analyst.

R Programming Challenges and Solutions

Here are the major R programming challenges and solutions for beginners.

Data Frame Creation and Manipulation

Challenge: Create an R data frame, insert a new column, and subset rows based on a condition.

Real-time Application: This is a fundamental skill for any data analysis project. You would apply this to clean and organize raw data, like a spreadsheet of sales data, prior to analysis. 

For instance, you could construct a data frame of customer data, include a Profit column, and then filter to display only transactions above a threshold.

Code Solution:

# Create a data frame

sales_data <- data.frame(

  Product = c(“A”, “B”, “C”, “D”),

  Quantity = c(100, 150, 75, 200),

  Price = c(5, 7, 10, 4)

)

# Add a new column ‘Revenue’

sales_data$Revenue <- sales_data$Quantity * sales_data$Price

# Filter for products with Revenue greater than 800

high_revenue_products <- subset(sales_data, Revenue > 800)

print(sales_data)

print(high_revenue_products)

Basic Statistical Summary

Challenge: Find the mean, median, and standard deviation of a numeric vector.

Real-time Application: This is crucial in exploratory data analysis (EDA). A financial analyst would use this to gauge the average stock price, the middle value, and the variability of a stock over time.

Code Solution:

# Create a numeric vector

stock_prices <- c(55.2, 56.1, 54.8, 57.0, 56.5, 58.2, 57.5)

# Calculate and print summary statistics

mean_price <- mean(stock_prices)

median_price <- median(stock_prices)

sd_price <- sd(stock_prices)

cat(“Mean price:”, mean_price, “\n”)

cat(“Median price:”, median_price, “\n”)

cat(“Standard deviation:”, sd_price, “\n”)

Recommended: R Programming Course Online.

Data Visualization with ggplot2

Challenge: Create a scatter plot using the ggplot2 package from a data frame.

Real-time Application: Visualization is key to communication. A data researcher can use a scatter plot to plot the relationship between two variables, exam scores and hours studied, to determine trends or patterns.

Code Solution:

# Install and load the ggplot2 library if not already installed

# install.packages(“ggplot2”)

library(ggplot2)

# Create a sample data frame

exam_data <- data.frame(

  Hours_Studied = c(1, 2, 3, 4, 5, 6, 7),

  Exam_Score = c(50, 65, 70, 85, 90, 95, 100)

)

# Create a scatter plot

ggplot(exam_data, aes(x = Hours_Studied, y = Exam_Score)) +

  geom_point() +

  labs(title = “Exam Score vs. Hours Studied”, x = “Hours Studied”, y = “Exam Score”)

Handling Missing Data

Challenge: Find and manage missing values (NA) in a vector by replacing them with the mean of non-missing values.

Real-time Application: Cleaning data is a vital operation in any analysis workflow. In a survey data set, there could be missing answers (NA). A data scientist would have to deal with these missing values, maybe by filling them in with the mean answer, in order not to corrupt their models.

Code Solution:

# Create a vector with missing values

survey_responses <- c(25, 30, NA, 35, 40, NA, 50)

# Calculate the mean of non-missing values

mean_value <- mean(survey_responses, na.rm = TRUE)

# Replace NA values with the calculated mean

survey_responses[is.na(survey_responses)] <- mean_value

print(survey_responses)

Recommended: R Programming Tutorial for Beginners.

Conditional Logic and Loops

Challenge: Create a for loop that cycles through a vector and prints out a different message depending on whether a number is odd or even.

Real-time Application: Conditional logic is applied in nearly all programming. In the detection of financial fraud, a system would loop around transactions and warn if any exceed a threshold or come from a suspicious area.

Code Solution:

# Create a numeric vector

numbers <- c(1, 4, 7, 10, 13, 16)

# Loop through each number and check if it’s even or odd

for (num in numbers) {

  if (num %% 2 == 0) {

    print(paste(num, “is even”))

  } else {

    print(paste(num, “is odd”))

  }

}

Function Creation

Challenge: Create a function that will receive a temperature in Celsius and return it as Fahrenheit.

Real-time Application: Functions serve to make code reusable and structured. This particular example might be used inside a weather program or a script to process data that requires temperature units conversion for standardization.

Code Solution:

# Define the conversion function

celsius_to_fahrenheit <- function(celsius) {

  fahrenheit <- (celsius * 9/5) + 32

  return(fahrenheit)

}

# Example Usage

temp_celsius <- 20

temp_fahrenheit <- celsius_to_fahrenheit(temp_celsius)

cat(temp_celsius, “degrees Celsius is”, temp_fahrenheit, “degrees Fahrenheit.\n”)

Recommended: R Programming Interview Questions and Answers.

String Manipulation

Challenge: Write a count of how many times a certain word occurs in a sentence.

Real-time Application: This is a simple application in natural language processing (NLP). It may be applied in a search engine to tally keyword frequency or in a sentiment analysis program to tally positive or negative words.

Code Solution:

# Create a sample sentence

sentence <- “The quick brown fox jumps over the lazy dog. The dog is very lazy.”

word_to_find <- “the”

# Convert the sentence to lowercase and split it into words

words <- strsplit(tolower(sentence), ” “)[[1]]

# Count the occurrences of the word

word_count <- sum(words == word_to_find)

cat(“The word ‘”, word_to_find, “‘ appears”, word_count, “times.\n”)

Subsetting with Logical Operators

Challenge: Choose all rows from a data frame such that two conditions are true (e.g., Age > 30 AND City == “New York”).

Real-time Application: This is the core for database queries and filtering data. A marketing team would use this to generate a focused list of customers (e.g., age > 30 in a particular city) for a new advertising campaign.

Code Solution:

# Create a data frame

customers <- data.frame(

  Name = c(“John”, “Jane”, “Peter”, “Susan”),

  Age = c(25, 32, 45, 28),

  City = c(“New York”, “London”, “New York”, “Paris”)

)

# Subset the data frame based on two conditions

targeted_customers <- subset(customers, Age > 30 & City == “New York”)

print(targeted_customers)

Working with Lists

Challenge: Make a list that holds a numeric vector, a character vector, and a data frame. Access and print the data frame in the list.

Real-time Application: Lists are adaptable and can hold various types of data. They are usually employed to collect the output of a statistical model, which could consist of coefficients (numeric), a description of the model (character), and the residuals (data frame).

Code Solution:

# Create a list with different data types

my_list <- list(

  numbers = c(1, 2, 3, 4, 5),

  names = c(“A”, “B”, “C”),

  data_table = data.frame(

    ID = 1:2,

    Value = c(100, 200)

  )

)

# Access and print the data frame from the list

print(my_list$data_table)

Related: Data Science with R Programming Course Online.

Simple if-else Statement

Challenge: Create a program that identifies whether a number input by a user is positive, negative, or zero.

Real-time Application: This is a simple control flow structure employed in most applications. An income value check in a loan application system or a check in a game to see whether the player’s score is negative before closing the game are some examples of its usage.

Code Solution:

# Assign a variable (or get user input)

my_number <- -5

# Check the condition

if (my_number > 0) {

  print(“The number is positive.”)

} else if (my_number < 0) {

  print(“The number is negative.”)

} else {

  print(“The number is zero.”)

}

Explore: All Trending Software Training Courses.

Conclusion

You’ve now finished these R programming exercises, having earned practical experience manipulating and analyzing data. From data frame construction to data visualization using ggplot2, these exercises have cemented your grasp of R’s fundamental functions. This is a good foundation for your data science journey.

Want to level up your skills further? Our all inclusive R programming course in Chennai can assist you in mastering more advanced subjects such as statistical modeling and machine learning.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.