R Programming
Fundamentals for Beginners
An Introduction to R for
Data Analysis and Visualization
- P Prakati
- Assistant Professor in Mathematics
- Sri Ramakrishna College of Arts &
Science, Coimbatore.
Introduction to R
• R is a programming language and software
environment used for statistical analysis, data
visualization, and data science.
• It is interpreted, open-source, and freely available.
• Created in the early 1990s by Ross Ihaka and
Robert Gentleman at the University of Auckland.
• R was inspired by the S language (developed at Bell
Labs).
• It's now maintained by the R Core Team and a large
open-source community.
Real-Life Example: Online Shopping
• Imagine you manage an online store:
• Use R to analyze sales data and customer behavior.
• Plot which products are trending using charts.
• Calculate average ratings from customer reviews.
• Predict future sales trends using past data.
BASIC DATA TYPES IN R
Type Description Example Used For In R
Numbers
with or Prices,
Numeric without 5, 3.14, 100.0 weights, x <- 3.14
decimal percentages
points
Text or string Names,
"apple", name <-
Character values inside labels,
“Hi All” "Ravi"
quotes categories
Boolean TRUE,
Conditions, is_hot <-
Logical values: TRUE FALSE,
comparisons TRUE
or FALSE 5>2
Whole
numbers with Counts, ages,
Integer 10L, 50L age <- 25L
L (integer IDs
literal)
Variables and Assignment in R
Concept Explanation Example
Variable A name used to store a value for reuse x <- 10
Assignment <- and = are both used to assign values to
y = 5 or y <- 5
Operators variables
Start with a letter; can contain letters,
Naming Rules digits, . or _; case-sensitive total_score, X1
Displays the value of a variable with [1]
print() print(x)
output format
Concatenates and prints without extra
cat() cat("Value is", x)
formatting
Returns the class/type of the object (e.g.,
class() class(5) → "numeric"
"numeric", "character")
typeof(5L) → "integer
Returns how R stores the object internally
typeof() (e.g., "double", "integer")
Classes vs. Types in R
Aspect class(x) typeof(x)
Describes the object’s
Definition Describes the object’s abstraction
internal storage
Used in object-oriented programming Used to understand how R
Purpose
(like S3/S4 objects) stores data
Low-level internal type of
Level High-level label for the data object
the object
Example 1 class(3.14) → "numeric" typeof(3.14) → "double"
Example 2 class(5L) → "integer" typeof(5L) → "integer"
Example 3 class(TRUE) → "logical" typeof(TRUE) → "logical"
Example 4 class("R") → "character" typeof("R") → "character"
typeof(data.frame(...)) →
Example 5 class(data.frame(...)) → "data.frame"
"list"
Data Structures in R
• Vectors
• Lists
• Matrices
• Arrays
• Data frames
• Factors
BasicDescription
Structure Data Structures
Example in R
Use Case
Vector Ordered collection of c(1, 2, 3) Storing numeric
elements of the same type scores
Matrix 2D array with elements of matrix(1:6, Mathematical
the same type nrow=2) computations
List Collection of elements of list(1, 'a', TRUE) Grouped data of
different types mixed types
Data Frame Table-like structure with data.frame(name Datasets, tabular
rows and columns ='A', age=25) data
Factor Categorical data with factor(c('low','m Statistical
levels edium','high')) modeling
Operators in R
• Arithmetic: +, -, *, /, ^
• Relational: ==, !=, <, >, <=, >=
• Logical: &, |, !
Control Structures
• if, else, ifelse()
• for loop
• while loop
• repeat loop
• break and next
Functions in R
• Built-in functions
• User-defined functions: function()
• Arguments and return values
Basic Data Visualization
• plot(), hist(), boxplot(), barplot()
• Customizing: titles, labels, colors
Basic Data Manipulation with dplyr
• select(), filter(), mutate(), summarize(), group_by()
Summary & Tips
• R is powerful for data analysis
• Practice is key
• Use resources: cheatsheets, R-bloggers,
StackOverflow
References
• R Documentation: rdocumentation.org
• RStudio: posit.co
• CRAN: cran.r-project.org