R - Solved QB Unit-II
R - Solved QB Unit-II
2-mark questions
1. What are Table format Files? List its features.
able format files are structured data files where data is organized into rows and
columns. They are often used for storing and exchanging data. Features of table
format files include:
Tabular structure with rows and columns.
Data organized in a structured format.
Common file formats include CSV, TSV, Excel, and more.
You can read a CSV file in R using the read.csv() function. Here's an example:
data <- read.csv("data.csv")
You can read web-based files in R using functions like read.table(), read.csv(),
or by downloading the file with libraries like download.file() and then reading it.
Here's an example using read.csv():
url <- "https://example.com/data.csv"
data <- read.csv(url)
8. What the "search path" in R refers to.? How can you view the current
search path in R?
The "search path" in R is the order in which R looks for objects, variables, and
functions. You can view the current search path using the search() function:
search()
count <- 1
repeat {
if (count > 5) {
break # Terminate the loop when count is greater than 5
}
cat("Count:", count, "\n")
Vidyashree B Lecturer in BCA@Sharada
3
count <- count + 1
}
Lazy evaluation means that an expression is not evaluated or computed until its
value is actually needed. In R, this allows for efficient memory usage and can
improve performance in some cases. Here's an example:
x <- 1:10
y <- x * 2 # At this point, the multiplication is not performed, and y is a promise.
print(y) # When you print or use y, the multiplication is evaluated.
16.How can you measure the execution time of a specific piece of code in R?
you can measure the execution time of a code block in R using the system.time()
function. Wrap the code you want to time with this function.
You can detach or "unmount" packages in R using the detach() function. Here's
an example:
19.How the :: operator help to prevent masking when calling functions from
specific packages?
The :: operator allows you to specify which package's function you want to use,
preventing function masking or conflicts. It ensures that the function is called
from the specified package. For example:
5. Explain for loop with its varie es in R with syntax and example.
A for loop in R is used to iterate over a sequence (e.g., vector, list) and execute a
block of code for each element. Varie es of for loops include:
Basic for loop:
Syntax:
for (variable in sequence) {
# Code to execute for each element of the sequence
}
Example:
for (i in 1:5) {
print(i)
}
for loop with seq_along():
Syntax:
Vidyashree B Lecturer in BCA@Sharada
8
for (i in seq_along(sequence)) {
# Code to execute for each element of the sequence
}
Example:
names <- c("Alice", "Bob", "Charlie")
for (i in seq_along(names)) {
print(names[i])
}
for loop with seq_len():
Syntax:
for (i in seq_len(n)) {
# Code to execute for each element from 1 to n
}
Example:
n <- 3
for (i in seq_len(n)) {
print(i)
}
6. Explain while loop in R with syntax and example.
A while loop in R is used to repeatedly execute a block of code as long as a
specified condi on is TRUE.
Syntax:
while (condi on) {
# Code to execute while the condi on is TRUE
}
8. How do you define and call, user defined func ons in R? Explain with an
example.
You can define a user-defined func on in R using the func on() keyword. Here's
an example:
# Define a func on
my_func on <- func on(x, y) {
result <- x + y
return(result)
Vidyashree B Lecturer in BCA@Sharada
10
}
# Now, the custom mean func on masks the base R mean func on
data <- c(1, 2, 3)
Vidyashree B Lecturer in BCA@Sharada
12
result <- mean(data) # Calls the custom mean func on
Variable Masking: When a variable with the same name as a func on is
defined, it masks the func on.
Example:
# Define a variable named "mean"
mean <- 42
16.What is sca erplot? Explain single plot and matrix of plots, with an
example.
A sca erplot is used to visualize the rela onship between two numeric
variables. It displays data points as dots on a two-dimensional graph. Here are
two types of sca erplots:
Single Sca er Plot: A single sca er plot visualizes the rela onship between
two variables.
Example:
x <- c(1, 2, 3, 4, 5)
y <- c(3, 6, 4, 9, 7)
plot(x, y, main = "Sca er Plot Example", xlab = "X-axis", ylab = "Y-axis")
Matrix of Sca er Plots: A matrix of sca er plots displays the rela onships
between mul ple pairs of variables. It is useful for visualizing correla ons in a
dataset.
Example:
data <- data.frame(
x1 = rnorm(100),
x2 = rnorm(100),
x3 = rnorm(100)
)
pairs(data, main = "Matrix of Sca er Plots")