R Programming 2 MARKS
R Programming 2 MARKS
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
# Example:
min_value <- min(3, 7, 1, 9) #1
pmin_values <- pmin(c(1, 5, 3), c(2, 4, 6)) #1 43
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
# Example:
x <- 1:10
y <- c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3)
spline_fit <- spline(x, y, n = 100, method = "natural")
plot(x, y)
lines(spline_fit, col = "red")
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
R is a programming language and environment widely used for solving data science problems
and particularly designed for statistical computing and data visualization.
• Open source
• Interpreted (i.e., it supports both functional and object-oriented programming)
• Highly extensible due to its large collection of data science packages
• Functional and flexible
• Compatible with many operating systems
• Can be easily integrated with other programming languages and frameworks
• Allows powerful statistical computing
• Offers a variety of data visualization tools for creating publication-quality charts
• Equipped with the command-line interface
• Supported by a strong online community
1. Vector : a one-dimensional data structure used for storing values of the same data
type.
2. List: a multi-dimensional data structure used for storing values of any data type
and/or other data structures.
3. Matrix: a two-dimensional data structure used for storing values of the same data
type.
4. Data frame: a two-dimensional data structure used for storing values of any data
type, but each column must store values of the same data type.
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
6. What is a package in R?
An R package is a collection of functions, code, data, and documentation, representing an
extension of the R programming language and designed for solving specific kinds of tasks.
7. What is a factor in R?
A factor in R is a specific data type that accepts categories (aka levels) from a predefined
set of possible values. These categories look like characters, but under the hood, they are
stored as integers. Often, such categories have an intrinsic order.
8. What is RStudio?
RStudio is an open-source IDE (integrated development environment) that is widely used
as a graphical front-end for working with the R programming language starting from
version 3.0.1.
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
13. What types of loops exist in R, and what is the syntax of each type?
1. For loop: iterates over a sequence the number of times equal to its length (unless the
statements break and/or next are used) and performs the same set of operations on each
item of that sequence.
for (variable in sequence) {
operations
}
2. While loop: performs the same set of operations until a predefined logical condition (or
several logical conditions) is met—unless the statements break and/or next are used.
while (logical condition) {
operations
variable update
}
3. Repeat loop: repeatedly performs the same set of operations until a predefined break
condition (or several break conditions) is met.
repeat {
operations
if(break condition) {
break
}
}
To aggregate data in R, we use the aggregate() function. This function has the following
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
15. What is the difference between the functions apply(), lapply(), sapply(),
and tapply()?
• While all these functions allow iterating over a data structure without using loops
and perform the same operation on each element of it, they are different in terms
of the type of input and output and the function they perform.
• apply(): takes in a data frame, a matrix, or an array and returns a vector, a list, a
matrix, or an array. This function can be applied row-wise, column-wise, or both.
• lapply(): takes in a vector, a list, or a data frame and always returns a list. In the
case of a data frame as an input, this function is applied only column-wise.
• sapply(): takes in a vector, a list, or a data frame and returns the most simplified
data structure, i.e., a vector for an input vector, a list for an input list, and a matrix
for an input data frame.
• tapply(): calculates summary statistics for different factors (i.e., categorical data).
16. List and define the control statements in R.
There are three groups of control statements in R: conditional statements, loop statements,
and jump statements.
Conditional statements:
• if—tests whether a given condition is true and provides operations to perform if it's
so.
• if-else—tests whether a given condition is true, provides operations to perform if
it's so and another set of operations to perform in the opposite case.
• if... else if... else—tests a series of conditions one by one, provides operations to
perform for each condition if it's true, and a fallback set of operations to perform if
none of those conditions is true.
• switch—evaluates an expression against the items of a list and returns a value from
the list based on the results of this evaluation.
Loop statements:
• for—in for loops, iterates over a sequence.
• while—in while loops, checks if a predefined logical condition (or several logical
conditions) is met at the current iteration.
• repeat—in repeat loops, continues performing the same set of operations until a
predefined break condition (or several break conditions) is met.
Jump statements:
• next—skips a particular iteration of a loop and jumps to the next one if a certain
condition is met.
• break—stops and exits the loop at a particular iteration if a certain condition is
met.
• return—exits a function and returns the result.
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
17. What are correlation and covariance, and how do you calculate them in R
Covariance measures the degree to which two variables change together, while
correlation is a standardized measure of covariance that ranges from -1 to 1, indicating
the strength and direction of the relationship.
www.ourcreativeinfo.in
R PROGRAMMING 2 MARKS
www.ourcreativeinfo.in