Easy Sample R Code For Beginners A Step By Step Guide

Welcome, aspiring data scientists and analysts! You've taken the first step into the rewarding world of data analysis by deciding to learn R programming. This open-source language is highly versatile and widely used for statistics and graphics, making it an invaluable skill to possess. Let's dive into some sample R code for beginners, exploring basic syntax, data manipulation, and visualization.

How to Learn Coding for Beginners: Free Coding Guide (Start Learning Now!)
How to Learn Coding for Beginners: Free Coding Guide (Start Learning Now!)

R is known for its extensive ecosystem of packages, offering functionalities tailored to various tasks. We'll introduce some essential packages and commands, ensuring you have a solid foundation to build upon. Let's kickstart your R programming journey with these fundamental concepts!

Complete C++ Roadmap for Beginners šŸš€
Complete C++ Roadmap for Beginners šŸš€

R Basics and Environment Setup

To commence your R learning adventure, you first need to install and configure the R software on your system. Visit the official CRAN (Comprehensive R Archive Network) website, choose your operating system, and download the appropriate version.

R Programming Cheat Sheet JUST THE BASICS
R Programming Cheat Sheet JUST THE BASICS

Upon installation, you'll encounter the R console, where you'll input and execute your code. Familiarize yourself with this environment, understanding where to type commands and view output.

Your First R Code: Saying Hello

Learn R for beginners with our PDF
Learn R for beginners with our PDF

Let's start with an easy and fun example - printing "Hello, World!" in R.

Type the following command in the R console and press Enter:

print("Hello, World!")

The output should be: Hello, World!

Learn to Code With These 5 Online Coding Courses for Beginners
Learn to Code With These 5 Online Coding Courses for Beginners

Basic R Data Types

R supports various data types, including numeric, character, and logical. Let's explore each:

  • Numeric types: Integers and floating-point numbers. You can create these by simply typing the values in R, e.g., 42 or 3.14159.
  • Character strings: Wrapped in quotation marks, e.g., "Hello, World!" or "R is cool".
  • Logical: Boolean values represented as TRUE or FALSE.
the logo for r in 2 hours no ads
the logo for r in 2 hours no ads

Data Manipulation with dplyr

To handle and analyze data efficiently, we'll rely on the popular dplyr package from the tidyverse collection. Install and load it using:

Learn to Code Free: 120+ Best Websites for Beginners (2025) šŸ“š
Learn to Code Free: 120+ Best Websites for Beginners (2025) šŸ“š
Top Coding Tools for Beginners šŸ’»āœØ
Top Coding Tools for Beginners šŸ’»āœØ
Coding for Beginners Course | How To Do Coding | Coding Languages
Coding for Beginners Course | How To Do Coding | Coding Languages
a white sign with the words 75 r tutors - step by step guide on it
a white sign with the words 75 r tutors - step by step guide on it
Start Coding from Zero (Beginner Guide 2026)
Start Coding from Zero (Beginner Guide 2026)
R Cheatsheet - 2
R Cheatsheet - 2
ā€œC Programming Roadmap for Beginners šŸš€ | Step-by-Step Guide to Master Cā€
ā€œC Programming Roadmap for Beginners šŸš€ | Step-by-Step Guide to Master Cā€
Learn R By Intensive Practice – Part 2
Learn R By Intensive Practice – Part 2
Cheat Sheet - R Codes Cheat Sheet
Cheat Sheet - R Codes Cheat Sheet
10 Coding Tips Every Beginner Should Know šŸ’» | Learn to Code Faster
10 Coding Tips Every Beginner Should Know šŸ’» | Learn to Code Faster
an image of a computer screen showing the names and numbers of different types of computers
an image of a computer screen showing the names and numbers of different types of computers
Best Coding Tips for Beginners
Best Coding Tips for Beginners
30 Days Coding Challenge for Beginners (FREE Roadmap)
30 Days Coding Challenge for Beginners (FREE Roadmap)
How to Learn R Language with Ultimate R Cheet Sheet.
How to Learn R Language with Ultimate R Cheet Sheet.
a laptop with the text, what is coding? click here to start learning for beginners
a laptop with the text, what is coding? click here to start learning for beginners
Coding roadmap for beginners
Coding roadmap for beginners
10 Coding Tips for Beginners to Improve Fast
10 Coding Tips for Beginners to Improve Fast
What is Coding? Simple Explanation for Beginners
What is Coding? Simple Explanation for Beginners
R programming Codes Cheat Sheet
R programming Codes Cheat Sheet
Beginner Coding Roadmap 2026
Beginner Coding Roadmap 2026

install.packages("tidyverse")
library(tidyverse)

Here, we'll learn to filter, sort, and aggregate data using dplyr's intuitive grammar.

Data Filtering with dplyr

Consider a dataset mtcars, built-in with R, containing vehicle attributes.

Let's filter the data to include only cars with more than 100 horsepower:

filtered_data <-mtcars %>% filter(cyl == 4)

The data will now only contain cars with 4 cylinders.

Data Sorting and Summary

Now let's sort this data by miles per gallon (mpg) in descending order and display a summary statistics table:

sorted_data <- filtered_data %>% arrange(desc(mpg))
.summary(sorted_data)

Clearly see how dplyr simplifies data manipulation tasks. This newfound knowledge will undoubtedly enhance your data analysis agility.

Data Visualization with ggplot2

install.packages("ggplot2")
library(ggplot2)

Let's create a simple scatter plot of MPG vs. Horsepower (hp) using ggplot2:

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  labs(title = "MPG vs. Horsepower",
       x = "Horsepower (hp)",
       y = "Miles per Gallon (mpg)")

This plot showcases a linear relationship between MPG and horsepower, demonstrating the power of visual storytelling in data analysis.

Creating Custom Themes

To make your visualizations distinctive, you can create custom themes using ggplot2's theme functions:

theme_custom <- theme_minimal() +
  theme(panel.background = element_blank(),
        text = element_text(color = "darkblue"))

Now apply this theme to your scatter plot:

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  labs(title = "MPG vs. Horsepower",
       x = "Horsepower (hp)",
       y = "Miles per Gallon (mpg)") +
  theme_custom

Keep experimenting with different themes, colors, and shapes to develop your unique visual style!

Embarking on your R programming journey can be exciting, and we trust this introduction has equipped you with essential knowledge and sparked curiosity for further exploration. Start practicing these concepts daily, and you'll soon notice your skills and confidence growing. Happy coding!

Related Articles

Python Dump Json To File Pretty Decision Tree Sample Code In Python R Code For Data Analysis Simple Python Game Code Rock Paper Scissors Python Etl Script Example Simple Python Game Code For Kids Gurobi Python Example Code How To Run A Python Code From Cmd Sample Datasets For Analysis Python Histogram From Dataframe Column