📊 STATISTICS WITH R PROGRAMMING
Madurai Kamaraj University · II B.Sc. Mathematics, III Semester · TANSCHE Syllabus
ARO Study Circle
📑 Table of Contents
Chapter 1 · Introduction to R Programming
Syllabus: Features of R – Reserved words – Identifiers – Constants – Variables – Operators – Operator Precedence – Strings – Basic Data Types
What is R?
R is an open-source programming language and environment designed specifically for statistical computing and graphics. It is widely used by statisticians, data scientists, and researchers. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand. The name "R" comes from the first letter of their first names.
- Initial version released in 1995, stable beta in 2000.
- R inherits features from the S language.
- Core written in C, Fortran, and R itself.
- Freely available under GNU General Public License.
- Works on Linux, Windows, and Mac.
- Provides both command-line and graphical interfaces.
1.1 Features of R
R has become the world's most widely used statistics programming language because of its rich set of features. Unlike many other statistical tools, R is not just a point-and-click software — it is a full programming language. This means you can write scripts, create functions, and automate complex workflows.
Key Features Explained:
- Complete programming language: R supports decision-making (if-else), loops (for, while), recursion, and input/output. You can write programs just like in C or Python.
- Data handling: R can efficiently store and manipulate large datasets, which is essential for statistical analysis.
- Operators for complex data: R provides special operators that work directly on arrays, lists, vectors, and matrices without needing explicit loops.
- Large package ecosystem: Over 10,000 packages available on CRAN for everything from genetics to finance.
- High‑quality graphics: Publication‑ready plots, plus interactive graphics with plotly or shiny.
- Statistical techniques: Linear/nonlinear modelling, time‑series, clustering, classification, hypothesis testing, and more.
- Extensible: Write code in C, C++, Fortran, or Python and call it from R.
- Object‑oriented: Supports S3, S4, R6 for modular, reusable code.
1.2 Installing R
Before you can write R programs, you need to install the R software on your computer. R is free and runs on Windows, Mac, and Linux.
1.2.1 Windows Installation
- Download the Windows installer (e.g., R‑3.4‑win.exe) from CRAN.
- Double‑click the downloaded .exe and accept default settings.
- For 64‑bit Windows, both 32‑bit and 64‑bit versions are installed.
- Launch R via: C:\Program Files\R\R3.4.2\bin\i386\Rgui.exe
- Double‑click to open the R‑GUI console.
1.2.2 Linux Installation
$ yum install R
# Debian / Ubuntu
$ sudo apt-get install r-base
After installation, launch the interactive prompt by typing R in the terminal.
1.3 Running R Programs
There are three main ways to run R code: interactively, as a script file, or using a GUI.
1.3.1 Command Prompt (Interactive Mode)
<- is the assignment operator. The output [1] indicates a vector of length 1.
1.3.2 R Script File
Save code with .R extension and run with Rscript:
[1] "Hello World!"
1.3.3 Using GUI (RStudio or RGui)
- Open R‑GUI or RStudio.
- File → Open Script, select your .R file.
- Select lines, right‑click → Run line or selection (or Ctrl+R).
1.4 Comments in R
Comments start with # and are ignored by R.
1.5 Reserved Words
Reserved words (keywords) have special meaning and cannot be used as variable names.
| Reserved Words in R |
|---|
| if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_ NA_complex_ NA_character_ |
- if, else, repeat, while, for, in, next, break, function – control flow and functions.
- TRUE, FALSE – logical constants.
- NULL – absence of value.
- Inf – infinity (e.g., 1/0).
- NaN – Not a Number (e.g., 0/0).
- NA – Not Available (missing value).
1.6 Identifiers
Names for variables, functions, or objects. Rules:
- Letters (a‑z, A‑Z), digits (0‑9), period (.), underscore (_) allowed.
- First character must be a letter or a period; if starting with period, second cannot be a digit.
- Cannot use reserved words.
total, Sum, .date.of.birth, Sum_of_two, Rank2
tot@1, 2um, _prod, TRUE, .0wl
Best practice: use periods as separators (e.g., a.variable.name) — also acceptable: underscores or camelCase.
1.7 Constants
Constants (literals) are fixed values.
- Numeric: 3.14, 42 (default double).
- Integer: suffix L, e.g., 42L.
- Complex: suffix i, e.g., 3+4i.
Built‑in constants: pi, letters, LETTERS, month.name, month.abb.
1.8 Variables
A variable is a named storage location. R is dynamically typed — type is determined by the value assigned.
Assignment Operators
| Operator | Description |
|---|---|
| <-, = | Leftward assignment (most common: <-) |
| ->, ->> | Rightward assignment (rare) |
| <<-, ->> | Global assignment |
variable.2: Lotus Rose
variable.3: 0 1
Dynamic typing example:
Use ls() to list variables, rm() to remove.
1.9 Operators in R
1.9.1 Arithmetic Operators
| Operator | Description |
|---|---|
| + | Adds two vectors. |
| - | Subtracts second vector from the first. |
| * | Multiplies first vector with the second. |
| / | Divides the first vector with the second. |
| %% | Remainder (modulus) of first vector divided by second. |
| %/% | Integer division (quotient only). |
| ^ | Exponentiation. |
Difference = 8 18 26 37
Product = 20 40 120 120
Quotient = 5 10 7.5 13.33333
Remainder = 0 0 2 1
Integer Division = 5 10 7 13
Exponentiation = 100 400 810000 64000
1.9.2 Relational Operators
| Operator | Description |
|---|---|
| < | Checks if element of first vector is less than corresponding element of second. |
| > | Checks if greater than. |
| <= | Less than or equal to. |
| >= | Greater than or equal to. |
| == | Equal to. |
| != | Not equal to. |
10 20 30 40 Greater Than 25 2 30 3 FALSE TRUE FALSE TRUE
10 20 30 40 Less Than or Equal To 25 2 30 3 TRUE FALSE TRUE FALSE
10 20 30 40 Greater Than or Equal To 25 2 30 3 FALSE TRUE TRUE TRUE
10 20 30 40 Equal To 25 2 30 3 FALSE FALSE TRUE FALSE
10 20 30 40 Not Equal To 25 2 30 3 TRUE TRUE FALSE TRUE
1.9.3 Logical Operators
| Operator | Description |
|---|---|
| ! | Logical NOT (element‑wise). |
| & | Element‑wise logical AND. |
| && | Logical AND (first element only). |
| | | Element‑wise logical OR. |
| || | Logical OR (first element only). |
0 20 30 56 Element-wise AND 2 2 30 0 FALSE TRUE TRUE FALSE
0 20 30 56 AND (&&) first element only 2 2 30 0 FALSE
0 20 30 56 Element-wise OR 2 2 30 0 TRUE TRUE TRUE TRUE
0 20 30 56 OR (||) first element only 2 2 30 0 TRUE
1.9.4 Assignment Operators
| Operator | Description |
|---|---|
| <-, <<- | Leftwards assignment |
| ->, ->> | Rightwards assignment |
1.9.5 Miscellaneous Operators
| Operator | Description |
|---|---|
| : | Creates a sequence of numbers (e.g., 3:9 → 3 4 5 6 7 8 9) |
| %in% | Checks if an element belongs to a vector (returns TRUE/FALSE) |
| %*% | Matrix multiplication (covered later) |
1.10 Operator Precedence
Operators with higher precedence are evaluated first. When equal precedence, associativity determines order.
| Operator | Description | Associativity |
|---|---|---|
| ^ | Exponent | Right to Left |
| -x, +x | Unary minus, unary plus | Left to Right |
| %% %/% | Modulus, Integer Division | Left to Right |
| * / | Multiplication, Division | Left to Right |
| + - | Addition, Subtraction | Left to Right |
| < > <= >= == != | Relational Operators | Left to Right |
| ! | Logical NOT | Left to Right |
| & && | Logical AND | Left to Right |
| | || | Logical OR | Left to Right |
| -> ->> | Rightward assignment | Left to Right |
| <- <<- | Leftward assignment | Right to Left |
| = | Leftward assignment | Right to Left |
1.11 Strings
Strings are sequences of characters. Use single or double quotes.
Reading strings from user input
Assignment: <- (preferred) or =
Comments: # at start of line or after code
Print: print(x) or cat(x)
Check type: class(x) or typeof(x)
List variables: ls()
Remove variable: rm(x)
Sequence: start:end (e.g., 3:9)
Membership: value %in% vector
Read user input: readline(prompt = "...")
1.12 Basic Data Types
R has five basic data types: numeric, integer, complex, logical, character.
1.12.1 Numeric
Decimal numbers (default).
1.12.2 Integer
Whole numbers with L suffix.
• Arithmetic between integer and numeric yields numeric.
• Converting decimal to integer truncates (no rounding).
• Logical: TRUE → 1, FALSE → 0.
1.12.3 Complex
1.12.4 Logical
TRUE / FALSE. In numeric contexts, TRUE → 1, FALSE → 0.
1.12.5 Character
Text values in quotes.
📋 Summary Table: Basic Data Types
| Data Type | Description | Example |
|---|---|---|
| Numeric | Decimal numbers (default type) | x <- 10.5 |
| Integer | Whole numbers (use L suffix) | x <- 10L |
| Complex | Numbers with real and imaginary parts | x <- 3 + 4i |
| Logical | Boolean values (TRUE/FALSE) | x <- TRUE |
| Character | Text/string values | x <- "Hello" |
🔍 Type Checking and Conversion Functions
| Function | Purpose |
|---|---|
| class(x) | Returns the data type of x |
| is.numeric(x) | Checks if x is numeric |
| is.integer(x) | Checks if x is integer |
| is.complex(x) | Checks if x is complex |
| is.logical(x) | Checks if x is logical |
| is.character(x) | Checks if x is character |
| as.numeric(x) | Converts x to numeric |
| as.integer(x) | Converts x to integer |
| as.complex(x) | Converts x to complex |
| as.logical(x) | Converts x to logical |
| as.character(x) | Converts x to character |
📘 R Programming · Unit 1 · TANSCHE Syllabus
Prepared by ARO Study Circle
0 Comments