Introduction to R
R is a programming language and software environment for
statistical computing and graphics.
Developed by statisticians Ross Ihaka and Robert Gentleman in
1993.
Used extensively in data analysis, machine learning, and data
visualization.
Open-source and supported by the R Foundation.
Reading Data in R
[Link]("[Link]") – Reads data from a CSV file into a
dataframe.
[Link]("[Link]", header=TRUE) – General-purpose
data reader.
read_excel("[Link]") – Reads Excel files (requires
`readxl` package).
readLines("[Link]") – Reads text files line by line.
readRDS("[Link]") – Reads a single R object from a file.
• Example: data <- [Link]("[Link]")
• Example: [Link](data, "processed_students.csv")
Writing Data in R
[Link](dataframe, "[Link]") – Saves dataframe to a CSV
file.
[Link](dataframe, "[Link]", sep="\t") – Writes to a
delimited text file.
writeLines(text_vector, "[Link]") – Writes character vectors
line by line.
saveRDS(object, "[Link]") – Saves a single R object.
• Example: [Link]("dplyr")
• Example: library(dplyr)
R Libraries and Packages
Libraries extend R's capabilities with additional functions.
Install a package: [Link]("packageName")
Load a package: library(packageName)
Common packages: ggplot2 (graphics), dplyr (data
manipulation), tidyr (data tidying), caret (ML)
Basic R Commands
Assignment: x <- 5 or x = 5
Viewing data: head(), tail(), summary(), str()
Data access: dataframe[row, column], dataframe$column
Creating vectors: c(1,2,3), seq(1,10), rep(1,5)
• Example: students$age
• Example: students[1:5, ]
• Example: mean(c(10, 20, 30))
Conditional Statements: if-else
Structure:
if (condition) {
# code
} else {
# alternative code
}
Example:
x <- 7
if (x > 5) {
print("Greater than 5")
} else {
print("5 or less")
}
Can be extended with else if
• Example with else if:
x <- 70
if (x > 90) {
print("A grade")
} else if (x > 75) {
print("B grade")
} else {
print("C grade")
}
Looping in R - for, while, repeat
for loop:
for (i in 1:5) {
print(i)
}
while loop:
i <- 1
while (i <= 5) {
print(i)
i <- i + 1
}
repeat loop:
i <- 1
repeat {
print(i)
i <- i + 1
if (i > 5) break
}
• Use for loop to sum numbers:
sum <- 0
for (i in 1:5) {
sum <- sum + i
}
• Repeat until user input is valid
Functions in R
Functions are reusable blocks of code.
Syntax:
my_function <- function(arg1, arg2) {
result <- arg1 + arg2
return(result)
}
Function call: my_function(10, 5)
Use functions to organize and modularize code.
• Function to check even or odd:
even_odd <- function(x) {
if (x %% 2 == 0) return("Even") else return("Odd")
}
Function Arguments and Options
Arguments can have default values.
Example:
greet <- function(name = "Guest") {
print(paste("Hello", name))
}
Calling with and without arguments:
greet(), greet("Radhamani")
‘...’ used for flexible number of arguments in functions.
• Flexible function:
sum_all <- function(...) {
return(sum(...))
}
• Calling with multiple arguments:
sum_all(1, 2, 3, 4, 5)
Sample Script
Demonstration
data <- [Link]("[Link]")
if (nrow(data) > 0) print("Data loaded successfully")
for (i in 1:nrow(data)) print(data[i,])
mean_calc <- function(x) mean(x)
print(mean_calc(data$column_name))
• Use dplyr to filter data:
filtered <- dplyr::filter(data, Age > 18)