# Assign values to variables
x <- 10
y <- 5
sum_xy <- x + y
print(sum_xy) # Output: 15
a and assign it the value
50.b and assign it the value
25.a and b together and print the
result.ggplot2, which works like building with
LEGO blocks:
# Load ggplot2
library(ggplot2)
# Create a scatter plot
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
mpg
dataset.ggplot2 package.mpg dataset to create a scatter plot of
displ vs. hwy.class.dplyr helps filter, select, arrange, and summarize
data.# Load dplyr
library(dplyr)
# Filter cars with highway MPG greater than 30
mpg_filtered <- mpg %>% filter(hwy > 30)
head(mpg_filtered)
# Select only manufacturer and highway MPG
mpg_selected <- mpg %>% select(manufacturer, hwy)
head(mpg_selected)
# Find average highway MPG for each manufacturer
mpg_grouped <- mpg %>% group_by(manufacturer) %>% summarize(avg_hwy = mean(hwy))
head(mpg_grouped)
mpg dataset by class instead of
manufacturer.hwy for each class.read_csv())# Load the readr package
library(readr)
# Read a CSV file
data <- read_csv("data.csv")
# View the first few rows
head(data)
readr package.sales_data.csv.gather() and spread() in R# Convert wide to long format
long_grades <- grades %>% gather(key = "Subject", value = "Score", Math:English)
head(long_grades)
# Convert long to wide format
wide_grades <- long_grades %>% spread(key = "Subject", value = "Score")
head(wide_grades)
gather().spread().# Remove missing values
data_clean <- drop_na(data)
head(data_clean)
replace_na().# Separate a column
data_separated <- data %>% separate(Name, into = c("First", "Last"), sep = "_")
head(data_separated)
# Unite columns back into one
data_united <- data_separated %>% unite("Full_Name", First, Last, sep = " ")
head(data_united)
separate().unite().# Create a factor variable
fruit <- factor(c("Apple", "Banana", "Apple", "Orange", "Banana"))
print(fruit)
# Rename levels of a factor
fruit <- factor(fruit, levels = c("Apple", "Banana", "Orange"), labels = c("Red", "Yellow", "Orange"))
print(fruit)
lubridate package.# Load lubridate
library(lubridate)
# Convert a string into a date
date1 <- ymd("2024-03-20")
print(date1)
ymd() converts a
YYYY-MM-DD format string into an R Date object.# Extract year, month, and day
print(year(date1)) # Output: 2024
print(month(date1)) # Output: 3
print(day(date1)) # Output: 20
# Define a function to add two numbers
add_numbers <- function(x, y) {
return(x + y)
}
# Use the function
result <- add_numbers(10, 5)
print(result) # Output: 15
function(x, y) {} → Defines a function
with two inputs (x and y).return(x + y) → Returns the sum of
x and y.# Print numbers from 1 to 5
for (i in 1:5) {
print(i)
}
for (i in 1:5) → Loops through numbers
1 to 5.print(i) → Prints each number.# Create a simple data frame
data <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Score = c(90, 85, 88)
)
print(data)
data.frame() → Creates a structured
dataset.Name, Age,
Score).City,
Country, and Population.lm() for linear regression
modeling.# Create a dataset
heights <- data.frame(
height = c(150, 160, 170, 180, 190),
weight = c(50, 60, 70, 80, 90)
)
# Build a linear model
model <- lm(weight ~ height, data = heights)
print(summary(model))
lm(weight ~ height, data = heights) →
Predicts weight using height.summary(model) → Shows model
details.Experience
vs. Salary.