# Your First R Command
print("Hello, R!")

# Installing Packages
install.packages("ggplot2")
install.packages(c("dplyr", "ggplot2", "readr"))

# Loading Packages
library(ggplot2)
library(dplyr)
library(readr)

# Essential Packages for Beginners
install.packages("dplyr")
install.packages("tidyr")
install.packages("readr")
install.packages("readxl")
install.packages("ggplot2")
install.packages("tidyverse")

# Checking What's Installed
installed.packages()
search()
"ggplot2" %in% rownames(installed.packages())

# R as a Calculator
5 + 3
10 - 4
6 * 7
20 / 4
2^3
sqrt(16)

# Order of Operations
2 + 3 * 4
(2 + 3) * 4
(10 + 5) / 3 - 2^2

# Useful Math Functions
abs(-5)
round(3.14159, 2)
max(5, 10, 3, 8)
min(5, 10, 3, 8)

# Variables
my_age <- 25
my_name <- "Alex"
circle_area <- 3.14159 * 5^2

price <- 19.99
tax_rate <- 0.08
tax_amount <- price * tax_rate
total_cost <- price + tax_amount
print(total_cost)

# Variable Naming Examples
student_count <- 25
average_grade <- 87.5
course_name <- "Data 101"

x <- 25
studentcount <- 25
# 2students <- 25  # invalid (starts with number)

# Numeric Data
students_in_class <- 30
average_height <- 5.8
total_students <- students_in_class + 5

# Character Data
first_name <- "Sarah"
last_name <- "Johnson"
full_name <- paste(first_name, last_name)
print(full_name)

# Logical Data
is_student <- TRUE
has_graduated <- FALSE

age <- 20
is_adult <- age >= 18
print(is_adult)

# Vectors
test_scores <- c(85, 92, 78, 96, 88)
student_names <- c("Alice", "Bob", "Charlie", "Diana")
passed_exam <- c(TRUE, TRUE, FALSE, TRUE, TRUE)

# Functions for Numbers
numbers <- c(10, 15, 8, 22, 12, 18)
mean(numbers)
median(numbers)
sum(numbers)
length(numbers)
max(numbers)
min(numbers)

# Functions for Text
names <- c("Alice", "Bob", "Charlie")
length(names)
nchar("Alice")
toupper("hello")
tolower("HELLO")

# Getting Parts of Vectors
fruits <- c("apple", "banana", "orange", "grape")
fruits[1]
fruits[3]
fruits[1:3]
fruits[c(1, 4)]

# Function Structure Examples
round(3.14159, 2)
paste("Hello", "World", sep = " ")

# Common Script Structure Example
library(dplyr)
library(ggplot2)

data <- read.csv("filename.csv")

processed_data <- data %>%
  filter(age > 18) %>%
  summarize(average_score = mean(score))

ggplot(processed_data, aes(x = category, y = average_score)) +
  geom_bar(stat = "identity")

# Getting Help in R
help(mean)
?mean
??average
example(mean)
args(mean)
