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.

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!

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.

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

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!

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.

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:




















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!