KEMBAR78
R Inter | PDF | Control Flow | Data Type
0% found this document useful (0 votes)
12 views6 pages

R Inter

The document provides an overview of basic R concepts, including data types, variables, vectors, arrays, matrices, data frames, lists, functions, control structures, and data manipulation with dplyr. It also covers data visualization using ggplot2 and introduces advanced R topics such as object-oriented programming and regular expressions. Each concept is explained with definitions and examples for clarity.

Uploaded by

Md Yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

R Inter

The document provides an overview of basic R concepts, including data types, variables, vectors, arrays, matrices, data frames, lists, functions, control structures, and data manipulation with dplyr. It also covers data visualization using ggplot2 and introduces advanced R topics such as object-oriented programming and regular expressions. Each concept is explained with definitions and examples for clarity.

Uploaded by

Md Yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Basic R Concepts

What is R?
Answer: R is a programming language and software environment for statistical computing and
graphics.

What are the basic data types in R?


Answer: The basic data types in R are Numeric, Character, Logical, Integer, and Complex.

What is a variable in R?
Answer: A variable is a container for storing data values. Variables are assigned using the <-
operator.

How do you assign values to variables in R?


Answer: You can assign values using the <- operator. For example, x <- 5.

What is the difference between == and = in R?


Answer: == is used for comparison, while = is used for assignment.

Vectors and Arrays

What is a vector in R?
Answer: A vector is a one-dimensional collection of elements of the same type.

How do you create a vector in R?


Answer: You can create a vector using the c() function. Example: v <- c(1, 2, 3).

What is the difference between c() and vector()?


Answer: c() combines elements into a vector, while vector() initializes an empty vector of a
specified type.

What is an array in R?
Answer: An array is a multi-dimensional data structure, where all elements must be of the same
type.

How do you create a 2D array in R?


Answer: You can create a 2D array using the array() function. Example: arr <- array(1:6, dim =
c(2, 3)).

Matrices

What is a matrix in R?
Answer: A matrix is a two-dimensional data structure with rows and columns, where all elements
are of the same type.

How do you create a matrix in R?


Answer: You can create a matrix using the matrix() function. Example: m <- matrix(1:6, nrow = 2,
ncol = 3).

How do you access elements in a matrix?


Answer: Elements in a matrix can be accessed using row and column indices. Example: m[1, 2].

What is the difference between a vector and a matrix in R?


Answer: A vector is one-dimensional, while a matrix is two-dimensional.

Data Frames
What is a data frame in R?
Answer: A data frame is a two-dimensional data structure, like a table, where each column can
hold different types of data.

How do you create a data frame in R?


Answer: You can create a data frame using the data.frame() function. Example: df <-
data.frame(Name = c("John", "Jane"), Age = c(25, 30)).

How do you access columns in a data frame?


Answer: You can access columns by name. Example: df$Age.

How do you add a new column to a data frame?


Answer: You can add a new column by simply assigning it. Example: df$Gender <- c("M", "F").

Lists
What is a list in R?
Answer: A list is an ordered collection of objects that can contain different types of data, such as
vectors, matrices, and other lists.

How do you create a list in R?


Answer: You can create a list using the list() function. Example: my_list <- list(name = "John",
age = 25).

How do you access elements in a list?


Answer: You can access list elements using the $ operator or double square brackets [[ ]].
Example: my_list$name.
How do you add an element to a list?
Answer: You can add elements to a list by assigning them directly. Example: my_list$city <-
"New York".

Functions

What is a function in R?
Answer: A function is a block of reusable code that performs a specific task and can take
arguments and return values.

How do you define a function in R?


Answer: You define a function using the function() keyword. Example:
my_func <- function(a, b) {
return(a + b)
}

What is the purpose of the return() function?


Answer: The return() function is used to return a value from a function.

How do you pass arguments to a function?


Answer: You pass arguments inside the function call. Example: my_func(3, 5).

Control Structures

What are control structures in R?


Answer: Control structures such as if, else, for, while, and repeat are used to control the flow of
execution in R.

How do you write an if statement in R?


Answer: An if statement is written as follows:
if (x > 5) {
print("Greater than 5")
}

How do you write an else statement in R?


Answer: An else statement can be written after an if statement:
if (x > 5) {
print("Greater than 5")
} else {
print("Less than or equal to 5")
}

What is a for loop in R?


Answer: A for loop is used to iterate over a sequence of elements. Example:
for (i in 1:5) {
print(i)
}

What is a while loop in R?


Answer: A while loop runs as long as a specified condition is true. Example:
while (x < 10) {
x <- x + 1
}

Apply Family Functions


What is the apply() function in R?
Answer: apply() is used to apply a function to the rows or columns of a matrix or array. Example:
apply(m, 1, sum) # Applies sum function to rows of matrix m

What is the difference between lapply() and sapply()?


Answer: lapply() returns a list, while sapply() tries to simplify the result to an array or matrix if
possible.

What does vapply() do in R?


Answer: vapply() is similar to sapply(), but it requires you to specify the type of the return value
to ensure consistency.

How does the mapply() function work in R?


Answer: mapply() applies a function to multiple arguments in parallel, typically used for
vectorized operations.

Data Manipulation with dplyr

What is the dplyr package in R?


Answer: dplyr is a package for data manipulation that provides a set of functions to efficiently
manipulate data frames.

How do you filter data using dplyr?


Answer: You can filter data using the filter() function. Example:
df_filtered <- filter(df, Age > 25)

How do you arrange data using dplyr?


Answer: You can arrange data using the arrange() function. Example:
df_arranged <- arrange(df, Age)

How do you group data in dplyr?


Answer: You can group data using the group_by() function. Example:
df_grouped <- group_by(df, Gender)
How do you summarize data using dplyr?
Answer: You can summarize data using the summarise() function. Example:
df_summary <- summarise(df, avg_age = mean(Age))

Data Visualization

What is ggplot2 in R?
Answer: ggplot2 is a popular R package for data visualization, providing a powerful and flexible
grammar of graphics.

How do you create a scatter plot in R?


Answer: You can create a scatter plot using the plot() function or ggplot2. Example with ggplot2:
ggplot(data, aes(x = Age, y = Height)) + geom_point()

What is a histogram in R?
Answer: A histogram is a graphical representation of the distribution of numerical data. Example:
hist(df$Age)

How do you create a bar plot in R?


Answer: You can create a bar plot using the barplot() function or ggplot2. Example with ggplot2:
ggplot(data, aes(x = Gender)) + geom_bar()

What is the difference between plot() and ggplot()?


Answer: plot() is a base R function for simple plots, while ggplot() is part of the ggplot2 package,
which is more flexible and powerful for complex visualizations.

Advanced R Topics

What is object-oriented programming (OOP) in R?


Answer: OOP in R is a programming style that organizes code into objects (data) and methods
(functions). R supports both S3 and S4 classes.

What are S3 and S4 classes in R?


Answer: S3
is a simpler, informal object-oriented system, while S4 is a more formal
system for defining classes and methods.
How do you create an S3 class in R?
Answer: You can create an S3 class using the class() function. Example:
my_object <- list(a = 1, b = 2)
class(my_object) <- "MyClass"

How do you create an S4 class in R?


Answer: You can create an S4 class using the setClass() function. Example:
setClass("Person", slots = c(name = "character", age = "numeric"))

What are regular expressions in R?


Answer: Regular expressions are patterns used to match strings in text. Functions like grep(),
gsub(), and regexpr() are used for pattern matching.

You might also like