Bda. Unit. 5
Bda. Unit. 5
ANS:-
1. Arithmetic Operators :-
(e.g., +, -, *, /, ^)
1.Arithmetic Operators (e.g., +, -, *, /, ^)
Used for basic mathematical operations.
2.Relational Operators (e.g., ==, !=, >, <, >=, <=)
Used to compare values.
3.Logical Operators (e.g., &, |, !, &&, ||)
Used for logical (Boolean) operations.
4.Assignment Operators (e.g., <-, =, ->)
Used to assign values to variables.
}
repeat: Repeats a block of code indefinitely until explicitly
stopped (using break).
EXAMPLE :-
count <- 1
repeat {
print(count)
count <- count + 1
if (count > 5) {
break
}
}
ANS:- In R, a function is a block of organized, reusable code that performs a specific task.
Functions are defined using the function keyword.
Syntax of a function in R:
function_name <- function(arguments) {
# Code to execute
return(result)
}
Example: Let's define a function to add two numbers:
add_numbers <- function(a, b) {
sum <- a + b
return(sum)
}
ANS :- a vector is a basic data structure that contains elements of the same type (like all
numbers, all characters, or all logical values).
Vectors are the most common and simplest type of data structure in R.
ANS:-
Matrix List
A matrix is a two-dimensional A list is a collection of elements
collection of elements of the same that can be of different data types
data type (numeric, character, etc.). (numbers, characters, vectors, even
other lists).
Organized into rows and columns. Ordered collection (like a
container) — no strict structure like
rows and columns.
All elements must be of the same Elements can be of mixed types.
type
Created using the matrix() function. Created using the list() function.
ANS:- In R, a factor is a data structure used for categorical data — data that can
take on a limited number of unique values, called levels.
Factors can be ordered (like "Low", "Medium", "High") or unordered (like "Red", "Green",
"Blue").
Statistical modeling: Many modeling functions in R (like lm(), glm()) automatically treat factors
correctly without needing manual intervention.
print(gender)
Ans:- In R, tables are often created using the table() function, which is used to create frequency
tables — that is, to count the number of occurrences of different values in a dataset.
# Create a vector
# Create a table
print(color_table)
Output: colors
3 1 2
11.How can we take user input in R?
In R, you can take user input using the readline() function. This function prompts the user to
enter a value as a string from the console. Here's the basic syntax:
If you want to convert the input to a numeric type (e.g., integer or double), you can wrap it with
as.numeric() or as.integer():
Graphical Capabilities of R:
R is renowned for its powerful and flexible graphical capabilities, making it a popular choice for
data visualization and statistical graphics. It supports both base graphics and more advanced
systems like lattice and ggplot2.
Base Graphics: Built into R, allows quick and simple plotting with functions like plot(),
hist(), boxplot(), and barplot().
Lattice Package: Offers a high-level data visualization system for creating multi-panel
plots and conditioning plots.
ggplot2 Package: Based on the Grammar of Graphics, it is widely used for building
complex, aesthetically pleasing, and layered visualizations.
R also supports:
The apply family of functions in R is used for applying a function to elements of data
structures like vectors, matrices, lists, and data frames without using explicit loops. These
functions help write concise, efficient, and readable code for repetitive operations.
apply(): Used for matrices or data frames to apply a function over rows (MARGIN = 1)
or columns (MARGIN = 2).
lapply(): Applies a function to each element of a list (or vector) and returns a list.
lapply(list_data, mean)
sapply(): Same as lapply(), but tries to simplify the result to a vector or matrix if possible.
sapply(list_data, mean)
tapply(): Applies a function over subsets of a vector, defined by a factor or grouping variable.
These functions are used to avoid explicit for loops and are often more efficient and expressive.
The lapply() function in R is used to apply a function to each element of a list or vector, and it
returns the result as a list—regardless of the output type of the function applied.
Can also be used on data frames (as they are lists of columns).
print(result)
Output:
$a
[1] 3
$b
[1] 8
$c
[1] 13
1.Explain the features of R programming language. Why is it widely used in data analysis?
Features of R Programming Language:
1. Open Source:
R is free to use and open-source, which allows for constant community contributions and wide
adoption.
3. Statistical Analysis:
R was built for statistics. It includes a wide range of built-in statistical techniques like linear
modeling, time-series analysis, classification, and clustering.
4. Graphical Capabilities:
R excels in data visualization with powerful libraries like ggplot2, lattice, and base plotting
functions, enabling both simple and complex graphics.
6. Cross-Platform Compatibility:
R works on Windows, macOS, and Linux, ensuring wide usability.
7. Integration:
R can integrate with other languages (like C, C++, Python) and tools (like Excel, SQL, Hadoop),
making it versatile in real-world applications.
8. Community Support:
A large, active user community contributes to tutorials, documentation, and new packages.
Flexible and Extensible: Easily adapted for specialized analytical tasks through packages.
Interactive Development Environment: Tools like RStudio make it user-friendly and productive.
Types of Operators in R
R provides a variety of operators to perform different types of operations on data. These can be broadly
classified into the following categories:
1. Arithmetic Operators
+ Addition 5+3→8
- Subtraction 5-3→2
* Multiplication 5 * 3 → 15
/ Division 6/2→3
^ or ** Exponentiation 2^3 → 8
Modulus
%% 7 %% 3 → 1
(remainder)
== Equal to 5 == 5 → TRUE
Greater or
>= 5 >= 5 → TRUE
equal
3. Logical Operators
& Element-wise AND c(TRUE, FALSE) & c(TRUE, TRUE) → TRUE FALSE
` ` Element-wise OR
&& Logical AND (first element only) TRUE && FALSE → FALSE
4. Assignment Operators
= Alternative to <- x = 10
5. Miscellaneous Operators
: – Sequence operator
1:5 # Output: 1 2 3 4 5
3.Explain control statements in R programming. Discuss if, else if, switch, and loops with examples.
ANS:-
Control statements in R are used to control the flow of execution of a program based on conditions or
repetitive tasks. They allow decision-making, branching, and iteration.
1. if Statement
Syntax:
if (condition) {
Example:
x <- 5
if (x > 0) {
print("x is positive")
2. if...else Statement
Syntax:
if (condition) {
# code if TRUE
} else {
# code if FALSE
}
Example:
x <- -3
if (x > 0) {
print("Positive number")
} else {
print("Non-positive number")
Example:
x <- 0
if (x > 0) {
print("Positive")
} else if (x < 0) {
print("Negative")
} else {
print("Zero")
4. switch Statement
Used for selecting one of many code blocks based on the value of an expression.
Syntax:
switch(expression,
case1 = {...},
case2 = {...},
...
Example:
option <- 2
switch(option,
"Add",
"Subtract",
"Multiply",
"Divide"
# Output: "Subtract"
5. Loops in R
for (i in 1:5) {
print(i)
i <- 1
while (i <= 5) {
print(i)
i <- i + 1
i <- 1
repeat {
print(i)
i <- i + 1
if (i > 5) break
Example:
for (i in 1:5) {
if (i == 3) next
print(i)
4.What are functions in R? How do you define and call a function? Explain with examples.
Functions in R
Functions in R are blocks of reusable code designed to perform a specific task. They help in modularizing
code, improving readability, and reducing repetition.
Defining a Function
# function body
# return(value) is optional
Calling a Function
You call a function by using its name followed by parentheses, with any required arguments:
function_name(value1, value2)
sum <- a + b
return(sum)
print(result)
Output:
[1] 8
Example 2: Function Without Return Statement
greet("Alice")
Output:
Hello, Alice!
Features of Functions in R:
5.Explain the concept of interfacing with R and discuss different ways R can be integrated with other
technologies.
Interfacing with R
Interfacing in R refers to the ability of R to communicate and work with other programming languages,
databases, and tools. This allows R to be part of a larger software ecosystem, enabling enhanced
functionality, speed, and integration in real-world applications.
1. R with C/C++
library(Rcpp)
add(3, 4)
2. R with Python
Use: Combine R's statistical power with Python's machine learning and automation tools.
Example:
library(reticulate)
py_run_string("x = 10")
3. R with Java
Example:
library(rJava)
.jinit()
Example:
library(DBI)
6. R with Excel
Example:
library(readxl)
6.Discuss in detail vectors, matrices, lists, and data frames in R with examples.
1. Vectors
A vector is a sequence of elements of the same type (numeric, character, logical, etc.). It's the most basic
data structure in R.
Creating a Vector:
Accessing Elements:
✅ 2. Matrices
A matrix is a 2-dimensional data structure (rows and columns) with elements of the same type.
Creating a Matrix:
Accessing Elements:
Matrix Operations:
t(mat) # Transpose
✅ 3. Lists
A list can contain elements of different types, including other lists, vectors, or even functions.
Creating a List:
my_list <- list(name = "John", age = 30, scores = c(90, 85, 88))
Accessing Elements:
my_list$name # "John"
my_list[[2]] # 30
✅ 4. Data Frames
df <- data.frame(
Accessing Data:
🔁 Comparison Summary:
7.What are factors and tables in R? How are they created and used?
In R, factors and tables are essential data structures used for categorical data analysis and data
summarization, especially in statistics and data science.
✅ 1. Factors in R
A factor is a data structure used to store categorical data (data that takes on a limited number of distinct
values or "levels").
Creating a Factor:
print(color_factor)
Output:
Ordered Factor:
Useful Functions:
levels(factor_variable) – Show levels
✅ 2. Tables in R
A table in R is an object that represents the frequency count of factor levels or combinations of levels.
Creating a Table:
print(color_table)
Output:
colors
2 1 2
table(gender, group)
Output:
group
gender A B
F 11
M 21
R supports various ways to read input from users or files and output data to the console or files. These
operations are fundamental for data analysis and automation.
print(nums)
head(data)
library(readxl)
x <- 42
print(x)
sink("log.txt")
print(summary(data))
✅ Summary Table:
Read text table read.table() Load data from delimited text files
9.How do you create graphs in R? Explain with examples using plot(), barplot(), and hist() functions
Creating Graphs in R
R has powerful graphical capabilities for both basic and advanced data visualization. Some of the most
commonly used base R functions for plotting include plot(), barplot(), and hist().
Used to create scatter plots, line graphs, and more depending on the data type.
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 1, 6, 3)
plot(x, y, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis", col = "blue", pch = 16)
Used to create vertical or horizontal bar charts from numeric vectors or tables.
barplot(counts, names.arg = names, col = "purple", main = "Bar Plot", ylab = "Frequency")
barplot(counts, names.arg = names, horiz = TRUE, col = "orange", main = "Horizontal Bar Plot")
3. hist() – Histogram
Example: Histogram
data <- c(18, 22, 25, 30, 26, 24, 22, 28, 32, 31)
hist(data, main = "Histogram of Data", xlab = "Values", col = "skyblue", border = "black")
Summary Table
10.What is the R apply family? Explain apply(), lapply(), sapply(), and tapply() functions with examples.
The apply family in R provides a set of functions to apply operations over various data structures (like
vectors, matrices, lists, and data frames) without using explicit loops. These functions make code more
concise and often more efficient.
Returns a vector or matrix if possible. Same as lapply(), but tries to simplify output.
sapply(nums, mean)
Output:
a b
3 8
Used for grouped operations, often in data analysis. Apply a function over subsets of a vector based
on grouping.
Example:
Output:
Female Male
22.5 25.3