Question BANK - R Programming
Question BANK - R Programming
UNIT I
2 Mark Questions
1. Define CRAN.
2. What is R Programming?
R is a programming language and software environment used for statistical computing, data
analysis, and graphics. It is open-source, supports a wide range of statistical techniques, and
provides powerful tools for data visualization and manipulation.
Data visualization is the graphical representation of data using charts, graphs, and maps. It
helps to understand trends, patterns, and insights in data more easily by presenting complex
information in a visual and intuitive way.
5. Define factor.
Example:
6. What is dataframe?
A data frame is a data structure in R used to store data in tabular form (rows and columns),
similar to a spreadsheet or SQL table. Each column can contain different data types (e.g.,
numeric, character, logical), but all columns must have the same number of rows.
Example:
df <- data.frame(
Name = c("Alice", "Bob"),
Age = c(25, 30),
Passed = c(TRUE, FALSE)
)
7. Define paste() .
The paste() function in R is used to combine multiple strings or variables into a single string.
It automatically inserts a space between the elements by default, but you can change the
separator using the sep argument.
Example:
An array is a more general data structure in R that can have two or more dimensions (e.g.,
2D, 3D, or higher). Like matrices, all elements in an array must be of the same data type.
matrix(1:6, nrow=2,
Example array(1:8, dim = c(2, 2, 2))
ncol=3)
9. Explain vector.
A vector in R is the simplest and most basic data structure. It is a one-dimensional collection
of elements that are all of the same data type (such as numeric, character, or logical).
Vectors are used to store sequences of data.
Example:
Coercion in R is the process of automatically converting one data type to another when
combining different types in a vector or expression. R follows a hierarchy of data types and
coerces to the most flexible type to avoid errors.
Example:
vec <- c(1, "apple", TRUE)
# Numeric 1 and logical TRUE are coerced to character, so vec becomes a character vector
Example:
In R, assigning values to variables is fundamental for storing and manipulating data. There
are three main ways to assign values to variables:
This is the most common and recommended method in R. It assigns the value on the right to
the variable on the left.
Example:
x <- 5
Works mostly the same as <- but can sometimes cause confusion inside function calls,
where = is used to assign function arguments.
Less preferred for variable assignment outside functions.
This operator assigns the value on the left to the variable on the right. It is less commonly
used but sometimes helpful in pipelines or when reading code bottom-to-top.
Example:
5 -> x
Additional Notes:
Variable names should start with a letter and can contain letters, numbers, dots, and
underscores.
In R, data types define the kind of data a variable can hold. R supports several basic data
types that are fundamental for data processing and analysis.
1. Numeric
3. Character
4. Logical
5. Complex
Example: 3 + 2i, 5 - 1i
6. Raw
They ensure correct operations (e.g., math on numeric types, text manipulation on
characters).
# Numeric
# Integer
int <- 5L
# Character
# Logical
# Complex
comp <- 2 + 3i
# Raw
In R, data structures organize and store data in different ways to enable efficient data
analysis and manipulation. They define how data is arranged and accessed. R provides
several fundamental data structures:
1. Vector
All elements must be of the same data type (numeric, character, logical, etc.).
2. Matrix
3. Array
4. List
5. Data Frame
Columns can contain different data types (numeric, character, factor, etc.), but each
column must be of a single type.
Example:
6. Factor:
Example:
# Print all
print(vec)
print(mat)
print(arr)
print(lst)
print(df)
In R, you can get input from the user during program execution using the readline() function.
This function reads a line of text from the console as a string.
The prompt argument displays a message asking the user to enter data.
The input is captured as a character string.
Since readline() returns a string, you often need to convert it to the required data type, such
as numeric or integer.
Convert to numeric:
age <- as.numeric(readline("Enter your age: "))
Convert to integer:
Store simple sequences of data like Store complex or mixed data like numbers,
Use Case
numbers or characters strings, vectors, or even other lists
Access elements by single index (e.g., Access elements by index or name (e.g.,
Access
vec[1]) lst[[1]] or lst$name)
Flexibility Less flexible due to uniform type More flexible, supports mixed data types
Vector Example:
print(vec)
print(vec[1]) # Output: 10
List Example:
lst <- list(name = "Alice", age = 25, scores = c(90, 85, 88))
print(lst)
print(lst$scores) # Output: 90 85 88
Operators in R are special symbols or keywords used to perform operations on variables and
values. They help in arithmetic calculations, comparisons, logical tests, and more.
1. Arithmetic Operators
Used to perform mathematical calculations.
+ Addition 5+3=8
- Subtraction 5-3=2
* Multiplication 5 * 3 = 15
/ Division 6/3=2
^ or ** Exponentiation 2^3=8
%% Modulus (remainder) 5 %% 2 = 1
== Equal to 5 == 5 → TRUE
Operator Description Example
3. Logical Operators
Used to combine or negate logical conditions.
4. Assignment Operators
Used to assign values to variables.
5. Miscellaneous Operators
1:5 generates
: Sequence generation
1,2,3,4,5
3 %in% c(1,2,3,4)
%in% Matches elements in vectors
→ TRUE
# Arithmetic Operators
a <- 10
b <- 3
# Relational Operators
# Logical Operators
# Assignment Operators
x <- 100
200 -> y
z = 300
# Miscellaneous Operators
# Print results
print(paste("Sum:", sum))
UNIT II
2 Mark Questions
UNIT III
2 Mark Questions
UNIT IV
2 Mark Questions
UNIT V
2 Mark Questions