UNIT 1
R Programming
1.7 R and R Studio
R and R Studio are powerful tools widely used for statistical analysis, data
visualization, and machine learning. While R is a programming language
and environment for data analysis, R Studio is an integrated development
environment (IDE) designed to make working with R more efficient and
user-friendly. Together, they form an essential toolkit for data scientists,
statisticians, and researchers.
What is R?
R is an open-source programming language and software environment for
statistical computing and graphics. It was developed in 1993 by Ross
Ihaka and Robert Gentleman at the University of Auckland, New Zealand.
Key Features of R:
1. Statistical Capabilities: Supports a wide range of statistical
techniques, including linear and nonlinear modeling, time series
analysis, and hypothesis testing.
2. Data Visualization: Offers high-quality graphical representations
such as histograms, box plots, and scatter plots.
3. Extensibility: Supports thousands of user-contributed packages for
advanced analysis.
4. Platform Independent: Works on Windows, Mac, and Linux.
5. Community Support: A large, active community contributes to its
growth, offering extensive documentation and forums.
What is R Studio?
R Studio is an IDE that provides a graphical interface for R, simplifying the
process of writing and executing R code. It enhances productivity by
integrating tools for coding, debugging, and visualization.
Key Features of R Studio:
1. Code Editor: Provides syntax highlighting, code completion, and
error detection.
2. Environment Pane: Displays active variables, datasets, and
objects in memory.
3. Console Pane: Allows direct interaction with R for running
commands.
4. Plots Pane: Displays visualizations created using R.
5. Packages Pane: Manages installation and loading of R packages.
6. History Pane: Logs previously executed commands for easy
reference.
Setting Up R and R Studio
Installing R:
1. Visit the Comprehensive R Archive Network (CRAN) website:
https://cran.r-project.org/.
2. Download the appropriate version for your operating system
(Windows, Mac, or Linux).
3. Follow the installation instructions specific to your platform.
Installing R Studio:
1. Visit the R Studio website: https://posit.co/.
2. Download the free R Studio Desktop version.
3. Install it after ensuring that R is already installed.
First Steps After Installation:
1. Open R Studio, which will automatically detect your R installation.
2. Use the Console Pane to run basic commands, such as 2 + 2.
3. Explore the other panes to familiarize yourself with the
environment.
Data Structures in R
R supports several data structures that form the foundation of data
manipulation and analysis:
1. Vector: A sequence of data elements of the same type (e.g.,
numeric, character).
o Example: v <- c(1, 2, 3, 4)
2. Matrix: A two-dimensional array of elements of the same type.
o Example: m <- matrix(1:6, nrow = 2, ncol = 3)
3. List: A collection of objects of different types.
o Example: lst <- list(name = "John", age = 30, scores = c(85,
90, 95))
4. Data Frame: A tabular data structure with columns of different
types.
o Example: df <- data.frame(Name = c("Alice", "Bob"), Age =
c(25, 30))
5. Factor: Represents categorical data.
o Example: factor_var <- factor(c("low", "medium", "high"))
R Syntax and Basic Expressions
R syntax is intuitive and straightforward, making it accessible for
beginners.
Basic Commands:
1. Arithmetic Operations:
o Addition: 3 + 2
o Multiplication: 4 * 5
2. Variable Assignment:
o Use <- or = to assign values: x <- 10 or y = 20.
3. Comments:
o Use # for comments: # This is a comment.
Data Manipulation:
1. Create a vector: vec <- c(10, 20, 30)
2. Indexing: vec[2] retrieves the second element.
3. Conditional Filtering: vec[vec > 15] retrieves elements greater than
15.
Packages in R
Packages are collections of functions and datasets that extend R's
capabilities. The CRAN repository hosts thousands of packages.
Installing Packages:
1. Install a package: install.packages("dplyr")
2. Load a package: library(dplyr)
3. Update packages: update.packages()
Popular Packages:
1. dplyr: For data manipulation.
2. ggplot2: For data visualization.
3. tidyr: For reshaping data.
4. caret: For machine learning.
Benefits of Using R and R Studio
1. Comprehensive Toolset: Combines statistical computing and
advanced graphics.
2. Open Source: Freely available and highly customizable.
3. Large Community: Offers extensive documentation, tutorials, and
forums.
4. Integration: Works seamlessly with databases, APIs, and other
programming languages.
5. Reproducibility: Facilitates sharing and collaboration through R
scripts and notebooks.
Common Challenges and Best Practices
Challenges:
1. Steep Learning Curve: Requires time to master advanced
features.
2. Performance: May be slower than other tools for large-scale
computations.
Best Practices:
1. Organize Code: Use comments and consistent formatting.
2. Learn Packages: Familiarize yourself with widely-used packages.
3. Save Work Regularly: Save scripts and outputs to avoid data loss.
4. Leverage Community Resources: Participate in forums and
online courses.
Real-World Applications
1. Data Analysis: Analyze customer trends, sales, and operational
metrics.
2. Visualization: Create insightful charts for business presentations.
3. Machine Learning: Develop predictive models for classification
and regression.
4. Statistical Research: Perform hypothesis testing and inferential
statistics.
1.8 Data Structure in R
Data structures in R are fundamental building blocks for storing and
manipulating data. They provide a way to organize, access, and process
data efficiently. R offers various data structures that cater to different
data types and use cases, ranging from simple one-dimensional vectors to
complex multi-dimensional arrays and lists.
Types of Data Structures in R
R has six primary data structures:
1. Vectors
2. Matrices
3. Arrays
4. Lists
5. Data Frames
6. Factors
Each serves specific purposes and supports particular data operations.
Vectors
A vector is a one-dimensional array that holds elements of the same data
type (numeric, character, or logical).
Creating a Vector:
# Numeric vector
numeric_vector <- c(1, 2, 3, 4)
# Character vector
char_vector <- c("apple", "banana", "cherry")
# Logical vector
logical_vector <- c(TRUE, FALSE, TRUE)
Operations on Vectors:
# Arithmetic operations
numeric_vector + 2 # Adds 2 to each element
# Indexing
numeric_vector[2] # Access the second element
# Logical filtering
numeric_vector[numeric_vector > 2] # Elements greater than 2
Matrices
A matrix is a two-dimensional array where all elements must be of the
same data type.
Creating a Matrix:
# Create a matrix with 2 rows and 3 columns
matrix_example <- matrix(1:6, nrow = 2, ncol = 3)
print(matrix_example)
Output:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Operations on Matrices:
# Access elements
matrix_example[1, 2] # Element in the 1st row and 2nd column
# Row and column sums
rowSums(matrix_example)
colSums(matrix_example)
Arrays
An array extends matrices to multiple dimensions.
Creating an Array:
# Create a 3-dimensional array
array_example <- array(1:12, dim = c(2, 3, 2))
print(array_example)
Accessing Array Elements:
array_example[1, 2, 1] # Element in 1st row, 2nd column of the 1st
matrix
Lists
A list can store objects of different types (vectors, matrices, even other
lists).
Creating a List:
# Create a list
list_example <- list(
Name = "John",
Age = 25,
Scores = c(85, 90, 95)
print(list_example)
Accessing List Elements:
# Access elements using $
list_example$Name
list_example$Scores[2] # Second score
Data Frames
A data frame is a table-like structure where columns can have different
data types. It is commonly used for storing datasets.
Creating a Data Frame:
# Create a data frame
data_frame_example <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Scores = c(80, 90, 85)
print(data_frame_example)
Output:
Name Age Scores
1 Alice 25 80
2 Bob 30 90
3 Charlie 35 85
Accessing Data Frame Elements:
# Access a column
data_frame_example$Age
# Access specific rows and columns
data_frame_example[1, ] # First row
data_frame_example[, "Name"] # Name column
Factors
A factor is used to handle categorical data in R.
Creating a Factor:
# Create a factor
factor_example <- factor(c("Low", "Medium", "High", "Low"))
print(factor_example)
Levels and Frequency:
# Check levels
levels(factor_example)
# Table of frequencies
table(factor_example)
Comparison of Data Structures
Data Dimensio Homogeneous Data
Common Use Case
Structure n Type?
Storing a sequence of
Vector 1D Yes
values
Numerical or logical
Matrix 2D Yes
computations
Array nD Yes Multidimensional data
Combining different data
List 1D No
types
Data Frame 2D No Tabular data (datasets)
Factor 1D No Categorical variables
Common Use Cases and Applications
1. Vectors: Storing numeric data for calculations.
2. Matrices: Performing linear algebra operations.
3. Arrays: Handling multidimensional datasets.
4. Lists: Organizing related but diverse data.
5. Data Frames: Preparing datasets for analysis and visualization.
6. Factors: Encoding categorical data for modeling.
1.8 R syntax
Key Characteristics:
1. Case Sensitivity: R is case-sensitive (var is different from Var).
2. Commands and Statements: Statements are separated by a
newline or a semicolon (;).
3. Comments: Use # to add comments in the code.
# This is a comment
x <- 10 # Assigning value 10 to variable x
Basic Arithmetic Operations:
# Arithmetic
addition <- 5 + 3 # Addition
subtraction <- 10 - 4 # Subtraction
multiplication <- 6 * 7 # Multiplication
division <- 16 / 4 # Division
exponent <- 2^3 # Exponentiation (2 raised to the power 3)
modulus <- 17 %% 5 # Modulus (remainder of division)
integer_division <- 17 %/% 5 # Integer division
Output:
addition: 8
subtraction: 6
multiplication: 42
division: 4
exponent: 8
modulus: 2
integer_division: 3
Variables and Assignments
Assigning Values:
Variables in R store data for later use. Use <-, =, or -> to assign values.
# Using <- for assignment
x <- 42
# Using = for assignment
y = 36
# Using -> for assignment
58 -> z
Rules for Naming Variables:
1. Must start with a letter or a period (.).
2. Can contain letters, numbers, underscores (_), and periods (.).
3. Cannot use reserved keywords like if, for, etc.
.valid_var <- 10 # Valid
2invalid_var <- 20 # Invalid
Viewing Variable Content:
print(x) # Prints the value of x
cat("The value of y is:", y, "\n") # Combines text and variable value
Data Types in R
R supports multiple data types:
1. Numeric: Numbers (e.g., 10, 3.14).
2. Integer: Whole numbers (e.g., 5L).
3. Character: Text strings (e.g., "Hello").
4. Logical: Boolean values (TRUE, FALSE).
5. Complex: Complex numbers (e.g., 3 + 2i).
num <- 3.14 # Numeric
int <- 5L # Integer
char <- "R Programming" # Character
logical <- TRUE # Logical
complex <- 2 + 3i # Complex
Checking Data Types:
class(num) # Returns "numeric"
is.integer(int) # Checks if the variable is integer
is.logical(logical) # Checks if the variable is logical
Operators in R
R has several types of operators:
Arithmetic Operators:
See the Basic Arithmetic Operations section above.
Relational Operators:
a <- 10
b <- 20
a == b # Equal to
a != b # Not equal to
a > b # Greater than
a < b # Less than
a >= b # Greater than or equal to
a <= b # Less than or equal to
Logical Operators:
x <- TRUE
y <- FALSE
x & y # Logical AND
x | y # Logical OR
!x # Logical NOT
Assignment Operators:
<-, =, ->
Miscellaneous Operators:
vec <- c(1, 2, 3, 4)
3 %in% vec # Membership (TRUE if 3 is in vec)
"hello" %in% vec # FALSE, since "hello" is not in vec
R Expressions
Expressions are combinations of values, variables, operators, and
functions.
# Example expression
result <- (5 + 3) * 2 - 4
print(result)
Evaluating Expressions:
Use the eval() function.
expr <- expression((5 + 3) * 2 - 4)
eval(expr)
Built-in Functions
R comes with numerous built-in functions to perform common tasks.
Examples:
sqrt(16) # Square root of 16
log(100, base=10) # Logarithm with base 10
round(3.14159, 2) # Round to 2 decimal places
Writing and Executing R Scripts
Interactive Mode:
Type commands directly in the R console.
Script Mode:
1. Save commands in a file with .R extension.
2. Run the script using:
source("script_name.R")
Common Errors in R Syntax
Missing Parentheses: Ensure matching parentheses in expressions.
print(10 + (5 * 2) # Missing closing parenthesis
Unquoted Strings: Strings must be enclosed in quotes (' or ")
name <- Hello # Error, should be name <- "Hello"
Incorrect Indexing: Indexing starts at 1, not 0.
vec <- c(1, 2, 3)
vec[0] # Error
Example Program
Program to Calculate Mean of a Vector:
# Define a numeric vector
numbers <- c(10, 20, 30, 40, 50)
# Calculate the mean
mean_value <- mean(numbers)
# Display the result
cat("The mean of the numbers is:", mean_value, "\n")
Output:
The mean of the numbers is: 30
1.9 Data Types in R
R is a dynamic and flexible language with several built-in data types
designed to handle various kinds of data. Understanding these data types
is essential for efficient data manipulation, analysis, and modeling.
Overview of Data Types in R
R supports the following primary data types:
1. Numeric: Numbers, including integers and real numbers.
2. Integer: Whole numbers.
3. Character: Text or string data.
4. Logical: Boolean values (TRUE or FALSE).
5. Complex: Complex numbers with real and imaginary parts.
6. Raw: Binary data.
Numeric Data Type
The numeric type is the default type for numbers in R. It includes both
integers and real numbers.
Examples:
# Numeric values
num1 <- 10 # Integer stored as numeric
num2 <- 3.14159 # Decimal value
# Check data type
class(num1) # Output: "numeric"
is.numeric(num2) # Output: TRUE
Operations on Numeric Data:
# Arithmetic operations
sum <- num1 + num2
product <- num1 * num2
cat("Sum:", sum, "Product:", product, "\n")
Integer Data Type
R represents integers explicitly by appending L to a number. Without L,
numbers are stored as numeric by default.
Examples:
# Integer value
int1 <- 25L # Explicitly declaring an integer
class(int1) # Output: "integer"
# Checking if a number is an integer
is.integer(int1) # Output: TRUE
is.integer(25) # Output: FALSE (stored as numeric)
Type Conversion to Integer:
num <- 10.7
int <- as.integer(num) # Convert to integer (truncates decimals)
print(int) # Output: 10
Character Data Type
The character type stores text strings.
Examples:
# Character data
char1 <- "R Programming"
char2 <- "Data Analysis"
# Check data type
class(char1) # Output: "character"
is.character(char2) # Output: TRUE
String Operations:
# Concatenation
greeting <- paste("Hello,", "Welcome to", char1)
print(greeting)
# String length
length_char <- nchar(char1) # Number of characters
cat("Length of char1:", length_char, "\n")
Logical Data Type
The logical type represents boolean values: TRUE and FALSE.
Examples:
# Logical values
logical1 <- TRUE
logical2 <- FALSE
# Check data type
class(logical1) # Output: "logical"
is.logical(logical2) # Output: TRUE
Logical Operations:
# AND, OR, NOT operations
result1 <- logical1 & logical2 # AND operation
result2 <- logical1 | logical2 # OR operation
result3 <- !logical1 # NOT operation
cat("AND:", result1, "OR:", result2, "NOT:", result3, "\n")
Complex Data Type
The complex type represents complex numbers with real and imaginary
parts.
Examples:
# Complex number
complex1 <- 2 + 3i
complex2 <- 1 - 4i
# Check data type
class(complex1) # Output: "complex"
# Arithmetic with complex numbers
sum_complex <- complex1 + complex2
product_complex <- complex1 * complex2
cat("Sum:", sum_complex, "Product:", product_complex, "\n")
Raw Data Type
The raw type is used to handle binary data.
Examples:
# Raw data
raw_data <- charToRaw("Hello")
print(raw_data) # Output: ASCII values in raw format
Type Conversion
R provides functions to convert between data types.
Examples:
# Convert numeric to character
num <- 123
char <- as.character(num)
# Convert character to numeric
char_num <- "456"
num_converted <- as.numeric(char_num)
# Convert numeric to logical
num_logical <- as.logical(0) # FALSE (0 is FALSE, non-zero is TRUE)
cat("Character:", char, "Numeric:", num_converted, "Logical:",
num_logical, "\n")
Checking Data Type
R has several functions to check the data type of a variable:
is.numeric()
is.integer()
is.character()
is.logical()
is.complex()
value <- 42.5
is.numeric(value) # Output: TRUE
is.integer(value) # Output: FALSE
Example Program
Program to Demonstrate Various Data Types:
# Define variables of different data types
num <- 10.5
int <- 10L
char <- "Data Science"
logical <- TRUE
complex <- 5 + 2i
# Print data types and values
cat("Numeric:", num, "Type:", class(num), "\n")
cat("Integer:", int, "Type:", class(int), "\n")
cat("Character:", char, "Type:", class(char), "\n")
cat("Logical:", logical, "Type:", class(logical), "\n")
cat("Complex:", complex, "Type:", class(complex), "\n")
Output:
Numeric: 10.5 Type: numeric
Integer: 10 Type: integer
Character: Data Science Type: character
Logical: TRUE Type: logical
Complex: 5+2i Type: complex
1.10 Operators
Operators are symbols used to perform operations on variables and
values in R. They are fundamental to data manipulation, computations,
and logical operations.
Types of Operators in R
R supports the following categories of operators:
1. Arithmetic Operators
2. Relational (Comparison) Operators
3. Logical Operators
4. Assignment Operators
5. Miscellaneous Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
Operato Outpu
Description Example
r t
+ Addition 5+3 8
- Subtraction 10 - 4 6
* Multiplication 6*7 42
/ Division 16 / 4 4
^ Exponentiation 2^3 8
Modulus
%% 17 %% 5 2
(Remainder)
%/% Integer Division 17 %/% 5 3
Examples:
# Arithmetic operations
x <- 10
y <- 4
add <- x + y
subtract <- x - y
multiply <- x * y
divide <- x / y
exponent <- x^2
modulus <- x %% y
int_div <- x %/% y
cat("Addition:", add, "Subtraction:", subtract, "\n")
cat("Multiplication:", multiply, "Division:", divide, "\n")
cat("Exponentiation:", exponent, "Modulus:", modulus, "Integer Division:",
int_div, "\n")
Relational (Comparison) Operators
Relational operators are used to compare values and return a logical value
(TRUE or FALSE).
Operato Exampl Outpu
Description
r e t
== Equal to 5 == 5 TRUE
!= Not equal to 5 != 3 TRUE
> Greater than 5>3 TRUE
< Less than 3<5 TRUE
Greater than or
>= 5 >= 5 TRUE
equal
<= Less than or equal 3 <= 5 TRUE
Examples:
# Relational operations
x <- 10
y <- 15
cat("x == y:", x == y, "\n") # FALSE
cat("x != y:", x != y, "\n") # TRUE
cat("x > y:", x > y, "\n") # FALSE
cat("x < y:", x < y, "\n") # TRUE
cat("x >= 10:", x >= 10, "\n") # TRUE
cat("y <= 20:", y <= 20, "\n") # TRUE
Logical Operators
Logical operators are used to perform Boolean operations.
Operato Descriptio Outpu
Example
r n t
& Logical AND TRUE & FALSE FALSE
` ` Logical OR `TRUE
! Logical NOT !TRUE FALSE
Examples:
# Logical operations
a <- TRUE
b <- FALSE
cat("a & b:", a & b, "\n") # FALSE
cat("a | b:", a | b, "\n") # TRUE
cat("!a:", !a, "\n") # FALSE
Vectorized Logical Operations:
Logical operations can also be performed on vectors.
vec1 <- c(TRUE, FALSE, TRUE)
vec2 <- c(FALSE, TRUE, TRUE)
result <- vec1 & vec2 # Logical AND
cat("Logical AND:", result, "\n") # FALSE, FALSE, TRUE
Assignment Operators
Assignment operators assign values to variables.
Operato Exampl
Description
r e
<- Leftward assignment x <- 10
Rightward
-> 10 -> x
assignment
= Assign value x = 20
Examples:
# Assignment examples
x <- 5 # Leftward assignment
y = 10 # Standard assignment
15 -> z # Rightward assignment
cat("x:", x, "y:", y, "z:", z, "\n")
Miscellaneous Operators
Miscellaneous operators include:
%in%: Checks membership in a vector.
:: Generates a sequence of numbers.
$: Extracts elements by name from lists or data frames.
Examples:
# Membership operator (%in%)
vec <- c(1, 2, 3, 4, 5)
cat("Is 3 in vec?", 3 %in% vec, "\n") # TRUE
cat("Is 6 in vec?", 6 %in% vec, "\n") # FALSE
# Sequence operator (:)
seq <- 1:5
cat("Sequence:", seq, "\n") # 1, 2, 3, 4, 5
# Extracting elements using $
data <- list(name = "Alice", age = 25)
cat("Name:", data$name, "Age:", data$age, "\n")
Operator Precedence
When multiple operators are used in an expression, R follows a
precedence order:
1. Parentheses ()
2. Exponentiation ^
3. Multiplication, Division *, /, %%, %/%
4. Addition, Subtraction +, -
5. Relational Operators (<, <=, etc.)
6. Logical Operators (&, |)
7. Assignment Operators <-, =, ->
# Operator precedence
result <- 10 + 5 * 2^2 / 5 - 3
cat("Result:", result, "\n")
Explanation:
1. 2^2 → 4
2. 5 * 4 → 20
3. 20 / 5 → 4
4. 10 + 4 → 14
5. 14 - 3 → 11
Common Errors with Operators
1. Unintended Precedence: Using operators without parentheses
can lead to unexpected results.
10 + 5 * 2 # Output: 20 (not 30)
Vector Recycling: Logical or arithmetic operations on vectors of different
lengths may lead to warnings.
c(1, 2, 3) + c(4, 5) # Recycles shorter vector
Example Program
Program to Perform Basic Operations:
# Define variables
x <- 15
y <- 5
# Arithmetic operations
add <- x + y
subtract <- x - y
multiply <- x * y
divide <- x / y
# Relational operations
greater <- x > y
equal <- x == y
# Logical operations
logical_and <- (x > 10) & (y < 10)
# Output results
cat("Addition:", add, "Subtraction:", subtract, "\n")
cat("Multiplication:", multiply, "Division:", divide, "\n")
cat("Is x > y?", greater, "Are x and y equal?", equal, "\n")
cat("Logical AND:", logical_and, "\n")
1.11 Basic expressions
Expressions in R are the building blocks of code execution. They combine
values, variables, and operators to compute results, perform assignments,
or invoke functions. Understanding basic expressions is critical for
efficient programming in R.
What is an Expression?
In R, an expression is any piece of code that can be evaluated to
produce a result. This includes:
1. Arithmetic operations.
2. Logical evaluations.
3. Assignments.
4. Function calls.
Syntax of an Expression:
# General structure
result <- operation(value1, value2, ...)
Arithmetic Expressions
Arithmetic expressions involve numeric computations using arithmetic
operators.
Examples:
# Simple arithmetic expressions
x <- 10
y <- 5
sum <- x + y # Addition
difference <- x - y # Subtraction
product <- x * y # Multiplication
quotient <- x / y # Division
power <- x^2 # Exponentiation
# Output results
cat("Sum:", sum, "Difference:", difference, "\n")
cat("Product:", product, "Quotient:", quotient, "Power:", power, "\n")
Logical Expressions
Logical expressions evaluate conditions and return TRUE or FALSE.
Examples:
# Logical comparisons
x <- 10
y <- 5
is_greater <- x > y # TRUE
is_equal <- x == y # FALSE
is_not_equal <- x != y # TRUE
# Output results
cat("Is x greater than y?", is_greater, "\n")
cat("Is x equal to y?", is_equal, "\n")
cat("Is x not equal to y?", is_not_equal, "\n")
Assignment Expressions
Assignment expressions store values in variables using assignment
operators.
Examples:
# Assignment expressions
x <- 42 # Assign value 42 to x
y <- x + 8 # Assign x + 8 to y
z <- "Hello" # Assign a string to z
# Rightward assignment
100 -> a
cat("x:", x, "y:", y, "z:", z, "a:", a, "\n")
unction Call Expressions
Function calls are expressions that execute predefined or user-defined
functions.
Examples:
# Predefined functions
sqrt_result <- sqrt(16) # Square root of 16
mean_value <- mean(c(1, 2, 3, 4, 5)) # Mean of a vector
# Output results
cat("Square Root:", sqrt_result, "Mean:", mean_value, "\n")
Vectorized Expressions
R is designed to work efficiently with vectors. Vectorized expressions
apply operations to entire vectors.
Examples:
# Vectorized arithmetic
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
result_add <- vec1 + vec2 # Element-wise addition
result_mult <- vec1 * vec2 # Element-wise multiplication
# Output results
cat("Vector Addition:", result_add, "\n")
cat("Vector Multiplication:", result_mult, "\n")
Combining Expressions
Multiple expressions can be combined in a single line using commas or
semicolons.
Examples:
# Combined expressions
x <- 10; y <- 20; z <- x + y
cat("x:", x, "y:", y, "z:", z, "\n")
Conditional Expressions
Conditional expressions evaluate conditions to execute code selectively.
Examples:
# Conditional expression
x <- 10
if (x > 5) {
cat("x is greater than 5\n")
} else {
cat("x is less than or equal to 5\n")
Expressions with Loops
Expressions can be part of loops to iterate computations.
Examples:
# Loop with expressions
sum <- 0
for (i in 1:5) {
sum <- sum + i
cat("Sum of numbers from 1 to 5:", sum, "\n")
Nested Expressions
Expressions can be nested to compute complex operations.
Examples:
# Nested expression
result <- sqrt((10 + 20) / 2)
cat("Nested Expression Result:", result, "\n")
Errors in Expressions
Common errors when writing expressions:
1. Syntax Errors: Missing parentheses or operators.
# Incorrect: Missing operator
result <- 10 +
Type Errors: Applying incompatible operations on data types.
# Incorrect: Cannot multiply character with numeric
result <- "text" * 2
Example Program
Demonstrating Various Expressions:
# Variables
a <- 15
b <- 5
vec <- c(2, 4, 6, 8)
# Arithmetic expressions
sum_ab <- a + b
prod_ab <- a * b
# Logical expression
is_even <- vec %% 2 == 0
# Function expression
mean_vec <- mean(vec)
# Combined and nested expressions
complex_result <- (sum_ab + prod_ab) / mean_vec
# Output
cat("Sum of a and b:", sum_ab, "\n")
cat("Product of a and b:", prod_ab, "\n")
cat("Vector is even:", is_even, "\n")
cat("Mean of vector:", mean_vec, "\n")
cat("Complex Result:", complex_result, "\n")
1.12 Installing and updating packages in R
Packages in R extend its capabilities by providing additional functions,
datasets, and tools for various tasks such as data visualization, statistical
modeling, machine learning, and more. Understanding how to install and
update packages is crucial for leveraging the full power of R.
What Are Packages in R?
Definition: A package is a collection of R functions, compiled code,
and sample data bundled together.
Sources: Packages are available on repositories like CRAN
(Comprehensive R Archive Network), Bioconductor, and GitHub.
Prerequisites for Installing Packages
1. Internet Connection: Required for downloading packages from
repositories.
2. R Version Compatibility: Ensure your R version supports the
desired package.
3. Package Repository: CRAN is the default repository, but others
like Bioconductor or GitHub may be used for specific packages.
Installing Packages
Packages are installed using the install.packages() function. You can
specify:
Package name(s).
Repository (default is CRAN).
Example 1: Installing a Single Package
# Install the 'ggplot2' package
install.packages("ggplot2")
# Load the package to use its functions
library(ggplot2)
Example 2: Installing Multiple Packages
# Install multiple packages
install.packages(c("dplyr", "tidyr", "readr"))
Example 3: Specifying a Custom Repository
# Install a package from a specific repository
install.packages("ggplot2", repos = "https://cloud.r-project.org")
Checking Installed Packages
Use installed.packages() to list all installed packages.
Example:
# Display all installed packages
installed_packages <- installed.packages()
head(installed_packages[, c("Package", "Version")])
Loading Installed Packages
To use the functions from an installed package, load it using library() or require().
Example:
# Load the 'dplyr' package
library(dplyr)
# Check if a package is available before loading
if (require("ggplot2")) {
print("ggplot2 is available and loaded.")
} else {
print("ggplot2 is not installed.")
Updating Packages
Updating ensures that you have the latest features and bug fixes.
Update a Single Package
# Update the 'dplyr' package
update.packages("dplyr")
Update All Packages
# Update all installed packages
update.packages(ask = FALSE) # Set ask = FALSE to update without confirmation
Removing Packages
Uninstall packages using the remove.packages() function.
Example:
# Remove the 'ggplot2' package
remove.packages("ggplot2")
Handling Installation Errors
Error: Package ‘X’ not available
o Ensure correct spelling and availability in the repository.
o Check R version compatibility.
Error: Dependency package(s) not available
o Install missing dependencies using dependencies = TRUE.
install.packages("package_name", dependencies = TRUE)
Post unit reading
1. Ken Black, 2013, Business Statistics, New Delhi, Wiley.