huzz
huzz
vec
Example:
vec
Example:
##Vectors
##Operations
sum_vec
Page 1 of 10
R Language Topics
4. Advanced Data Structures:
• Vectors: A vector in R is a one-dimensional data structure that holds an ordered
collection of elements of the same type (numeric, character, or logical).
Example:
vec <- c(1, 2, 3, 4, 5)
• Data Frame: A tabular structure where columns can have different data types.
Example:
D <- data.frame(Name = c("Ali", "Sara"), Age = c(25, 30), Score = c(85, 90))
Example:
Example:
L <- list(vec, D, M)
5. Control Structures: Are expressions used to control the execution and flow of the
program based on the conditions provided in the statements. These structures are used
to decide after assessing the variable.
6. if and else statements: The if and else statements in R are fundamental control
structures used to perform conditional execution of code blocks.
if (condition) {
An if statement checks a condition, and if
If # Code to execute if condition is TRUE
it is TRUE, the associated code block runs.
}
if (condition) {
# Code to execute if condition is TRUE
The else statement provides an alternative
Else } else {
action when the if condition is not met.
# Code to execute if condition is FALSE
}
Page 2 of 10
R Language Topics
7. ifelse statement:
The ifelse function in R is a vectorized
ifelse checks the condition for each element in
conditional statement that evaluates a logical
a vector and assigns different outcomes
condition for each element of a vector and
based on whether the condition is met (TRUE)
returns a specified value for TRUE and
or not (FALSE).
another for FALSE. It is useful for performing
Syntax:
element-wise conditional operations
ifelse(condition, true statement, false
efficiently.
statement)
Example:
• if-else Statement:
x <- 2
if (x > 3) {
} else {
• ifelse Function:
print(result)
8. For Loops in R: A for loop in R is used to repeat a block of code a specific number of
times. It is often used for iterating over a sequence of values (e.g., numbers, vectors, or
lists).
Example:
for (i in 1:5)
Page 3 of 10
R Language Topics
{
9. Data Entry in R: Data entry in R refers to the methods used to input data into R from
various sources, such as manual input, external files, or user input.
• Manual Entry:
print(data)
df <- read.csv("data.csv")
print(df)
• User Input:
D <- c()
D <- edit(D)
The Grammar of Graphics in R, implemented through the ggplot2 package, is a system for
creating complex, layered visualizations. It allows users to describe plots using a consistent
framework that includes data, aesthetic mappings, geometric objects, and statistical
transformations.
• Use: ggplot2 is used to create a wide range of visualizations (e.g., bar charts, line plots,
scatter plots) and is popular due to its flexibility and ability to create highly customizable
plots.
• Key Components:
Page 4 of 10
R Language Topics
o Data: The dataset you want to plot.
o Aesthetics (aes): Mappings of variables to visual properties like x and y axes, color,
shape, etc.
o Geometric Objects (geom): The actual elements of the plot, like points, lines, bars,
etc.
o Themes: Control the appearance of the plot (background color, grid lines, etc.).
Example:
library(ggplot2)
Use: pnorm() is useful for finding the cumulative probability up to a certain point in a
normally distributed dataset.
2. qnorm():The qnorm() function is the inverse of pnorm(). It computes the quantile for a
given cumulative probability. In other words, it finds the value corresponding to a specific
cumulative probability in a normal distribution.
Page 5 of 10
R Language Topics
Use: qnorm() is used to determine the value (or threshold) for a given probability
in a normal distribution.
3. dnorm() : The dnorm() function in R is used to calculate the probability density (height of
the probability density function) at a specific value for a normal distribution. It returns
the value of the normal distribution's probability density function (PDF) for a given point.
Use: You use dnorm() when you want to compute the density of the normal
distribution at a particular value. This is useful for understanding how likely or "dense" a
particular value is in a normal distribution.
4. rnorm() in R: The rnorm() function generates random samples from a normal distribution.
It is used to simulate data that follows the normal (Gaussian) distribution, defined by its
mean (μ) and standard deviation (σ).
Use: Use rnorm() when you need to create random data that approximates a
normal distribution, which is common in statistical modeling, testing, and simulations.
Syntax: rnorm(n, mean = 0, sd = 1)
Example:
rnorm(10)
Generate 5 random numbers from a normal distribution with μ=50 and σ=10:
Page 6 of 10
R Language Topics
# Output: e.g., 45.231, 60.123, 47.456, etc.
Use: You use dbinom() when you want to find the probability of observing exactly k
successes out of n trials.
Example: Find the probability of getting exactly 3 heads in 5 flips of a fair coin (probability of
heads = 0.5):
Use: You use pbinom() when you want to find the probability of getting k or fewer
successes out of n trials.
Example: Find the probability of getting 3 or fewer heads in 5 flips of a fair coin:
3. qbinom(): The qbinom() function computes the quantile (or threshold value) for a given
cumulative probability in a binomial distribution. In simpler terms, it answers the
question: "What is the number of successes below which the cumulative probability is p?"
Page 7 of 10
R Language Topics
Use: You use qbinom() when you want to find the minimum number of successes
required to achieve a given cumulative probability.
Syntax: qbinom(p, size, prob)
Example: Find the number of successes needed to reach at least 95% cumulative probability in a
binomial distribution with 10 trials and a success probability of 0.5:
# Output: 8
Example:
Generating Random Data: Simulate the number of heads obtained when flipping a fair coin 10
times, repeated 5 times:
# Output: e.g., 5, 6, 4, 7, 5
1. dpois():
Page 8 of 10
R Language Topics
• Syntax: dpois(k, lambda)
dpois(3, lambda = 5)
# Output: 0.1403739
2. ppois():
• Use Case: When you need the probability of observing up to a certain number of events.
ppois(3, lambda = 5)
# Output: 0.2650259
3. qpois():
• Purpose: To find the value of k (number of events) that corresponds to a given cumulative
probability.
• Use Case: When you need to determine the threshold for a specific cumulative probability.
• Example: Number of events needed to achieve a cumulative probability of 0.8 when λ=5:
qpois(0.8, lambda = 5)
# Output: 7
4. rpois():
• Use Case: When you need simulated data or random values from a Poisson process.
Page 9 of 10
R Language Topics
• Syntax: rpois(n, lambda)
rpois(10, lambda = 5)
Page 10 of 10