R Programming – Detailed Exam Notes
1. Introduction to R
- Definition:
R is a programming language and environment for statistical computing, data analysis, and graphics. - History:
Developed by Ross Ihaka and Robert Gentleman in 1993 at the University of Auckland. - Key Features:
- Free and open-source.
- Large package repository (CRAN).
- Highly extensible for statistical methods and machine learning.
- Strong data visualization capabilities.
- Platform independent (Windows, Linux, macOS).
- Community-driven development.
2. Getting R and Managing R
a) Installing R
- Download from CRAN: https://cran.r-project.org/.
- Install RStudio IDE for user-friendly interface.
b) Package Management
-
Install a package:
install.packages("dplyr") -
Load a package:
library(dplyr) -
Remove a package:
remove.packages("dplyr") -
List installed packages:
installed.packages()
3. Arithmetic and Matrix Operations
a) Arithmetic Operators
| Operator | Description | Example (a=10, b=3) | Result |
|---|---|---|---|
+ | Addition | a+b | 13 |
- | Subtraction | a-b | 7 |
* | Multiplication | a*b | 30 |
/ | Division | a/b | 3.33 |
b | 1 | ||
%/% | Integer Division | a%/%b | 3 |
^ or ** | Power | a^b | 1000 |
b) Matrix Creation
m1 <- matrix(1:6, nrow=2, ncol=3, byrow=TRUE)
print(m1)Output:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
c) Matrix Operations
m2 <- matrix(7:12, 2, 3)
m1 + m2 # Addition
m1 - m2 # Subtraction
m1 * m2 # Element-wise multiplication
t(m1) # Transpose
m1 %*% t(m2) # Matrix multiplication (2x3 %*% 3x2)4. Functions in R
a) Built-in Functions
Examples:
mean(c(1,2,3,4,5)) # Mean
sum(1:5) # Sum
sqrt(16) # Square rootb) User-defined Functions
# Function with arguments and return
power <- function(x, y) {
result <- x^y
return(result)
}
power(2, 3) # 8c) Default Parameters
greet <- function(name="Guest") {
print(paste("Hello", name))
}
greet() # Hello Guest
greet("R User") # Hello R User5. Control Structures
a) If-Else
x <- -2
if (x > 0) {
print("Positive")
} else if (x < 0) {
print("Negative")
} else {
print("Zero")
}b) For Loop
for (i in 1:5) {
print(paste("Iteration:", i))
}c) While Loop
count <- 1
while (count <= 3) {
print(count)
count <- count + 1
}d) Repeat Loop (similar to do-while)
count <- 1
repeat {
print(count)
count <- count + 1
if (count > 3) break
}e) Switch Statement
choice <- "c"
result <- switch(choice,
"a" = "Apple",
"b" = "Banana",
"c" = "Cherry")
print(result)6. Working with Objects and Data
a) Types of Objects
| Object Type | Description | Example |
|---|---|---|
| Vector | One-dimensional array of same type | c(1,2,3) |
| Matrix | 2D array of same type | matrix(1:6,2,3) |
| List | Collection of elements (any type) | list(name="R", age=30) |
| Data Frame | Table-like structure (rows & columns) | data.frame(ID=1:3, Name=c("A","B","C")) |
| Factor | Stores categorical data | factor(c("M","F","M")) |
b) Manipulating Objects
x <- c(10, 20, 30)
x[2] # Access (20)
x[1] <- 15 # Modify element
length(x) # Lengthc) Constructing Data Objects
vec <- c(1,2,3,4) # Vector
mat <- matrix(1:9, 3, 3) # Matrix
lst <- list(name="R", year=1993) # List
df <- data.frame(ID=1:3, Score=c(90,85,88)) # Data Framed) Types of Data Items
- Numeric –
x <- 3.14 - Integer –
y <- 5L - Character –
z <- "Hello" - Logical –
flag <- TRUE - Complex –
c <- 2+3i
Check type:
typeof(x)
class(df)e) Structure of Data Items
str(df)
summary(df)Output:
'data.frame': 3 obs. of 2 variables:
$ ID : int 1 2 3
$ Score: num 90 85 88
f) Reading and Getting Data
-
CSV File:
data <- read.csv("data.csv", header=TRUE) head(data) -
Text File:
data <- read.table("data.txt", header=TRUE) -
Keyboard Input:
x <- readline(prompt="Enter a number: ") as.numeric(x)
g) Manipulating Data
df$Score[2] <- 95 # Modify value
subset(df, Score > 90) # Filter rows
df$Grade <- c("A", "B", "A") # Add new column
df[order(df$Score, decreasing=TRUE), ] # Sorth) Storing Data
save(df, file="data.RData") # Save R object
load("data.RData") # Load R object
write.csv(df, "output.csv") # Export to CSVQuick Diagram: Objects in R (Hierarchy)
Object
│
├── Atomic Vectors (numeric, character, logical, complex)
├── Matrix (2D homogeneous)
├── Array (multi-dimensional homogeneous)
├── List (heterogeneous collection)
└── Data Frame (table, columns = vectors)