Question Bank Answers
Question Bank Answers
iv. mean(7:10)
The sequence 7:10 is 7, 8, 9, 10.
The mean is the sum of these numbers divided by the number of elements:
mean = (7 + 8 + 9 + 10) / 4 = 34 / 4 = 8.5
v. median(7,8,9,10)
The median is the middle value of a set of numbers.
For the sequence 7, 8, 9, 10, there are 4 numbers, so the median is the average of
the two middle values, which are 8 and 9.
median = (8 + 9) / 2 = 17 / 2 = 8.5
1. Numeric
Descrip on: Represents numbers, which can be integers or real numbers (floa ng-
point numbers).
Decimal values are called numeric in R.
It is the default computa onal data type.
If we assign a decimal value to a variable x as follows, x will be of numeric type.
If we assign an integer to a variable k, it is s ll being saved as numeric value.
The fact that if k is an integer can be confirmed with the is.integer() func on
Examples:
num1 <- 42 # An integer
num2 <- 3.14 # A floa ng-point number
num3 <- -7.5 # A nega ve number
2. Integer
Descrip on: Represents whole numbers explicitly specified as integers. Use the L
suffix to denote an integer.
Examples:
int1 <- 10L # Integer
int2 <- -25L # Nega ve integer
is.integer(int1) # TRUE
3. Character
Descrip on: Represents text or string values. Strings are enclosed in quotes (single or
double).
The Character object is used to represent string values in R.
Objects can be converted into character values using the as.character() func on.
A paste() func on can be used to concatenate two character values
Examples:
char1 <- "Hello" # A string
char2 <- 'World' # Another string
paste(char1, char2) # "Hello World"
4. Logical
Descrip on: Represents Boolean values (TRUE or FALSE).
When two variable are compared, the logical values are created.
The logical operators are “&”(and), “|”(or) and “!”(nega on/not).
Examples:
logical1 <- TRUE # Logical value TRUE
logical2 <- FALSE # Logical value FALSE
logical3 <- 5 > 3 # TRUE
5. Complex
Descrip on: Represents complex numbers with real and imaginary parts.
If we find the square root of -1, it gives an error. But if it is converted into a complex
number and then square root is applied, it produces the necessary result as another
complex number.
Examples:
complex1 <- 2 + 3i # Complex number (2 is the real part, 3 is the imaginary part)
complex2 <- 5 - 1i # Complex number
Example Code in R:
# Numeric
num <- 3.14
print(num)
# Integer
int <- 5L
print(int)
# Character
char <- "Data Science"
print(char)
# Logical
logical_val <- TRUE
print(logical_val)
# Complex
comp <- 4 + 5i
print(comp)
R automa cally assigns the appropriate data type when you create variables, but you can
check or convert data types using func ons like is.numeric(), is.character(), as.integer(), etc.
4. Explain the following flow control statements in R with suitable examples for each
i. If and else statement
ii.Switch Statement
ANS:
i. If and else statement
If statement :takes a logical value and executes the next statement only if the value is
TRUE
a <- 33
b <- 200
if (b > a) {
print("b is greater than a")
}
In this example we use two variables, a and b, which are used as a part of the if statement to
test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than
33, and so we print to screen that "b is greater than a".
Else If: The else if keyword is R's way of saying "if the previous condi ons were not
true, then try this condi on"
a <- 33
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a = = b) {
print ("a and b are equal")
}
In this example a is equal to b, so the first condi on is not true, but the else if condi on is
true, so we print to screen that "a and b are equal".
You can use as many else if statements as you want in R.
If Else: The else keyword catches anything which isn't caught by the preceding
condi ons:
a <- 200
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a == b) {
print("a and b are equal")
} else {
print("a is greater than b")
}
In this example, a is greater than b, so the first condi on is not true, also the else if condi on
is not true, so we go to the else condi on and print to screen that "a is greater than b".
Ifelse() :func on takes three arguments of which the first is logical condi on, the
second is the value that is returned when the first vector is TRUE and third is the value that
is returned when the first vector is FALSE.
ii.Switch Statement
• If there are many else statements, it looks confusing and in such cases the
switch()func on is required.
• The first argument of the switch statement is an expression that can return a string
value or an integer.
• This is followed by several named arguments that provide the results when the name
matches the value of the first argument.
• Here also we can execute mul ple statements enclosed by curly braces.
• If there is no match the switch statement returns NULL. So, in this case, it is safe to
men on a default value if none matches.
5. Find the output for the following
i. seq(1:5)
ii.length(1:7)
iii.rep(1:3,4)
iv.c(“Week”,”WEEK”,”week”,”weak”)==”week”
v.5:9%%2
Ans:
Let’s evaluate each statement step by step:
(i) seq(1:5)
The 1:5 generates the sequence 1, 2, 3, 4, 5.
The seq() func on treats 1:5 as a single vector, so seq(1:5) is equivalent to seq(c(1, 2, 3, 4,
5)). In this case, it outputs the elements of the vector unchanged.
Output:
1, 2, 3, 4, 5
(ii) length(1:7)
The 1:7 generates the sequence 1, 2, 3, 4, 5, 6, 7.
The length() func on returns the number of elements in this sequence.
Output:
7
(iii) rep(1:3, 4)
The rep() func on repeats the elements of the vector 1:3 a specified number of mes.
1:3 generates 1, 2, 3, and rep(1:3, 4) repeats this en re sequence 4 mes.
Output:
1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3
(v) 5:9 %% 2
The 5:9 generates the sequence 5, 6, 7, 8, 9.
The %% operator computes the remainder when each element is divided by 2:
5 %% 2 = 1
6 %% 2 = 0
7 %% 2 = 1
8 %% 2 = 0
9 %% 2 = 1
Output:
1, 0, 1, 0, 1
6.List the logical operators in R with suitable examples.
Ans:
Logical operators in R are used to perform logical (Boolean) opera ons. These operators
return logical values (TRUE or FALSE) and are o en used in condi onal statements and data
filtering. Here's a list of logical operators in R along with examples:
# && operator
TRUE && FALSE # Output: FALSE
TRUE && TRUE # Output: TRUE
2. OR (| and ||)
Descrip on: Returns TRUE if at least one condi on is TRUE.
|: Vectorized, works element-wise on vectors.
||: Only evaluates the first element of each vector.
Examples:
# | operator
c(TRUE, FALSE, TRUE) | c(FALSE, TRUE, FALSE) # Output: TRUE, TRUE, TRUE
# || operator
FALSE || TRUE # Output: TRUE
FALSE || FALSE # Output: FALSE
3. NOT (!)
Descrip on: Negates the logical value.
Returns: TRUE becomes FALSE, and FALSE becomes TRUE.
Examples:
!TRUE # Output: FALSE
!FALSE # Output: TRUE
!c(TRUE, FALSE, TRUE) # Output: FALSE, TRUE, FALSE
# Inequality
c(1, 2, 3) != c(1, 4, 3) # Output: FALSE, TRUE, FALSE
6. xor()
Descrip on: Exclusive OR; returns TRUE if one (and only one) of the two logical
values is TRUE.
Examples:
xor(TRUE, FALSE) # Output: TRUE
xor(TRUE, TRUE) # Output: FALSE
xor(FALSE, FALSE) # Output: FALSE
8.Explain repeat, while and for loop with R programing example (10)
Ans:
There are three kinds of loops in R namely
• Repeat
• While
• For
Repeat Loops
The repeat is the easiest loop in R that executes the same code un l it is forced to
stop.
This repeat is similar to the do while statement in other languages.
A break statement can be given when it is required to break the looping.
Also it is possible to skip the rest of the statements in a loop and executes the next
itera on and this is done by using the next statement.
Repeat with break statement
a<-1
repeat{
print(a)
a<-a+1
if(a==4){
break }}
Repeat with break and next statement
a<-0
repeat{
a<-a+1 if(a==4){
next
}
print(a) if(a==6){
break
}
}
While Loops
The while loops are backward repeat loops.
The repeat loop executes the code and then checks for the condi on, but in while
loops the condi on is first checked and then the code is executed.
So, in this case it is possible that the code may not be executed even once when the
condi on fails at the entry itself during the first itera on.
A break statement can be given when it is required to break the looping.
Also it is possible to skip the rest of the statements in a loop and executes the next
itera on and this is done by using the next statement.
While with break statement
a<-0
while(a<6){
a<-a+1
if(a==4){
break
}
print(a)
}
For Loops
• The for loops are used when we know how many mes the code needs to be
repeated.
• The for loop accepts an itera ng variable and a vector.
• It repeats the loop giving the itera ng each element from the vector in turn.
• In this case also if there are mul ple statements to execute, we can use the curly
braces.
• The itera ng variable can be an integer, number, character or logical vectors and they
can be even lists.
for(i in 1:5)
{
j<-i*i
message("The square value of ",i," is ",j)
}
10. Develop R code to calculate the following financial metrics in order to assess the
financial statement of an organiza on being supplied with 2 vectors of data: Monthly
Revenue =[50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 155, 165] and Monthly Expenses
=[30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85] for the financial year
(i)Profit for each month.
(ii)Profit a er tax for each month (Tax Rate is 30%).
(iii)Profit margin for each month equals to profit a er tax divided by revenue.
(iv)Good Months – where the profit a er tax was greater than the mean for the year.
(v). Bad Months – where the profit a er tax was less than the mean for the year.
vi. The best month – where the profit a er tax was max for the year.
vii. The worst month – where the profit a er tax was min for the year(10)
ANS:
Program no 2
11.Develop R Code to Perform the following:
a) Assign different type of values to variables and display the type of variable. Assign
different types such as Double, Integer, Logical, Complex and Character and understand
the difference between each data type.
b) Demonstrate Arithme c and Logical Opera ons with simple examples.
C) Demonstrate genera on of sequences and crea on of vectors.
d) Demonstrate Crea on of Matrices
e) Demonstrate the Crea on of Matrices from Vectors using Binding Func on.
f) Demonstrate element extrac on from vectors, matrices and arrays
ANS:
Program no- 1
• The func ons ls() and ls.str() take an environment argument and lists its contents.
• We can test if a variable exists in an environment using the exists() func on.
• An environment can be converted into a list using the func on as.list() and a list can
be converted into an environment using the func on as.environment() or the
func on list2env().
• All environments are nested and so every environment has a parent environment.
The empty environment sits at the top of the hierarchy without any parent. The
exists() and the get() func on also looks for the variables in the parent environment.
To change this behaviour we need to pass the argument inherits = FALSE.
The word frame is used interchangeably with the word environment. The func on to
refer to parent environment is denoted as parent.frame().
The variables assigned from the command prompt are stored in the global
environment. The func ons and the variables from the R's base package are stored in
the base environment
Func ons
A func on and its environment together is called a closure.
When we load a package, the func ons in that package are stored in the
environment on the search path where the package is installed.
Func ons are also another data types and hence we can assign and manipulate and
pass them as arguments to other func ons.
Typing the func on name in the command prompt lists the code associated with
the func on.
Below is the code listed for the func ons readLines() and matrix().
When we call a func on by passing values to it, the values are called as arguments.
The lines of code of the func on can be seen between the curly braces as body of
the func on.
In R, there is no explicit return statement to return values.
Example
In this cube is the name of the func on and x is the argument passed to this func on.
The content within the curly braces is the body of the func on. (Note: If it is a one line
code we can omit the curly braces). Once a func on is defined, it can be called like any
other func on in R by passing its arguments.
The func ons formals(), args() and formalArgs() can fetch the arguments defined
for a func on. The body of the func on can be retrieved using the body() and
deparse() func ons
Func ons can be passed as arguments to other func ons and they can be
returned from other func ons.
For calling a func on, there is another func on called do.call() in which we can
pass the func on name and its arguments as arguments.
The use of this func on can be seen below when using the rbind() func on to
concatenate two data frames.
13. Explain Ini a ng R with examples
ANS:
1.3 Ini a ng R
Open R GUI, find the command prompt and type the command below and hit enter to run
the command
>sum(1:5)
[1] 15
The result above shows that the command gives the result 15. That the command has taken
the input of integers from 1 to 5 and has performed the sum opera on on them.
In the above command sum() is a func on that takes the argument 1:5 which means a
vector that consists of sequence of integers from 1 to 5.
1.3.2 Help in R
• If a func on name or dataset name known then we can type ? followed by the name.
• If name is not known then we need to type ?? Followed by a term that is related to
the search func on.
• Keywords, special characters and two separate terms of search need to be enclosed
in double or single quotes.
• The values can be assigned to the variables using the symbol “< -” or “=“ of which the
symbol “<-” is preferred.
Examples
> x<-1:3
>x
[1] 1 2 3
> Y=4:6
>Y
[1] 4 5 6
> x+3*Y-2
[1] 11 15 19
• The variable names consists of le ers, numbers, dots and underscore, but a variable
name should only starts with an alphabet.
• To create a global variable (Variable available everywhere) we use the symbol “<<-”.
Example
> x<<-exp(exp(1))
>x
[1] 15.15426
• For global assignment the same func on assign() can be used, but ,by including an
extra a ribute globalenv().
• To see the value of the variable, simply type the name of variable in the command
prompt.
• If assignment and prin ng of a value has to be done in one line we can do the same
in two ways.
• The sta s cal func on in R can take the vectors as input and produce results.
The exponen a on operator is represented using the symbol “^” or the “**”.
The other mathema cal func on are the trigonometry func ons like sin(), cos(), tan(),
asin(), acos(), atan() and the logarithmic and exponen al func ons like log(), exp(),
loglp(), expml().
All these mathema cal func ons can operate on vectors as well as individual elements.
The other rela ons operators are the ”<”, “>”, “<=“, “>=“.
ANS:
p r in t ( ” Matrix A: ” )
p r in t (A)
p r in t ( ” Matrix B: ” )
p r in t (B)
transpose_B <-t(b)
p r in t ( ” Transpose o f Matrix A: ” )
p r in t ( transpose_A)
p r in t ( ” Transpose o f Matrix B: ” )
p r in t ( transpose_B)
C <-A+B
p r in t (C)
C <- A-B
p r in t (C)
p r in t ( ” Matrix M u l t ip l i c a t i o n o f A and B: ” )
p r in t (C)
print(first_row_A)
# Access by name
print(my_list$Gree ng) # Access the "Gree ng" element
print(my_list[["Matrix"]]) # Access the "Matrix" element
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
[4,] 7 10
[5,] 8 11
[6,] 9 12
5. Explain the date and me func on with examples (5)
Ans:
R provides several func ons to handle and manipulate date and me values. These
func ons are part of the base package and work with objects of class Date and POSIXt.
2. Date Conversion
as.Date(): Converts a character string to a Date object.
as.POSIXct() or as.POSIXlt(): Converts a character string to a date- me object with
me zones.
Example:
# Convert character to Date
date_string <- "2025-01-23"
date_object <- as.Date(date_string)
print(date_object)
# Format current me
forma ed_ me <- format(Sys. me(), "%H:%M:%S")
print(forma ed_ me)
Output:
[1] "23-01-2025"
[1] "10:25:30"
4. Date Arithme c
You can perform arithme c opera ons on Date and POSIXct objects.
Example:
# Add or subtract days
today <- Sys.Date()
next_week <- today + 7
last_week <- today - 7
7. Time Difference
di ime(): Calculates the difference between two dates or mes.
Example:
start_date <- as.Date("2025-01-01")
end_date <- as.Date("2025-01-23")
(i) gl()
The gl() func on generates factors by specifying the number of levels, repe ons, and
labels.
Code:
# Example: gl(3, 2)
output <- gl(3, 2, labels = c("A", "B", "C"))
print(output)
Output:
[1] A A B B C C
Levels: A B C
Explana on:
3 specifies the number of levels.
2 specifies the number of repe ons for each level.
labels assigns names to each level ("A", "B", "C").
(ii) paste(c("Pine", "Red"), "Apple", sep = "-")
The paste() func on concatenates strings, using the specified separator (sep).
Code:
paste(c("Pine", "Red"), "Apple", sep = "-")
Output:
[1] "Pine-Apple" "Red-Apple"
Explana on:
The first argument is a vector c("Pine", "Red").
Each element is combined with "Apple", separated by "-".
8. Determine the output of the following func ons applied on the given
dataframe.(5M)
x<-c(5,6,7,8)
y<-c(15,16,17,18)
z<-c(25,26,27,28)
G<-data.frame(x,y,z)
i) colSums(G[,1:2])
ii)colMeans(G[,1:3])
iii)rowSums(G[1:3,])
iv)rowMeans(G[2:4,]
v)colMeans(,G[1:2])
ANS:
Here’s the solu on to your query, step by step:
Given Dataframe
x <- c(5, 6, 7, 8)
y <- c(15, 16, 17, 18)
z <- c(25, 26, 27, 28)
G <- data.frame(x, y, z)
print(G)
Output:
x y z
1 5 15 25
2 6 16 26
3 7 17 27
4 8 18 28
(iii) rowSums(G[1:3,])
Code:
rowSums(G[1:3,])
Explana on:
G[1:3,] selects the first three rows of all columns.
rowSums() computes the sum of each row.
Output:
[1] 45 48 51
(iv) rowMeans(G[2:4,])
Code:
rowMeans(G[2:4,])
Explana on:
G[2:4,] selects rows 2 to 4 of all columns.
rowMeans() computes the mean of each row.
Output:
[1] 16 17 18
(v) colMeans(G[1:2,])
Code:
colMeans(G[1:2,])
Explana on:
G[1:2,] selects the first two rows of all columns.
colMeans() computes the mean of each column.
Output:
x y z
5.5 15.5 25.5
(ii) sprin ()
The sprin () func on is used for forma ed string crea on, similar to C-style string
forma ng.
Syntax:
sprin (fmt, ...)
Example:
sprin ("The result of %d + %d is %d", 3, 4, 3 + 4)
Output:
[1] "The result of 3 + 4 is 7"
Explana on:
%d is a placeholder for integers.
Addi onal placeholders include %f for floats and %s for strings.
(iii) strsplit()
The strsplit() func on splits a string into substrings based on a delimiter.
Syntax:
strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
Example:
strsplit("I like Banana, Orange, and Pineapple", split = ", ")
Output:
[[1]]
[1] "I like Banana" "Orange" "and Pineapple"
Explana on:
The string is split wherever the delimiter ", " is found.
(iv) substr()
The substr() func on extracts or replaces parts of a string based on start and end posi ons.
Syntax:
substr(x, start, stop)
Example:
substr("The quick brown fox", 5, 9)
Output:
[1] "quick"
Explana on:
Extracts characters from posi on 5 to 9.
(v) toupper()
The toupper() func on converts all characters in a string to uppercase.
Syntax:
toupper(x)
Example:
toupper("hello world")
Output:
[1] "HELLO WORLD"
Explana on:
Converts the en re string to uppercase le ers.
,,2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , Layer2
Col1 Col2 Col3
Row1 7 9 11
Row2 8 10 12
(i) with()
Theory: The with() func on allows you to evaluate an expression within the context of a
data frame or list, making it easier to refer to columns directly without repeatedly typing the
data frame name. It is useful for simplifying code when performing opera ons on mul ple
columns in a data frame.
Syntax:
with(data, expression)
data: A data frame or list.
expression: The R expression to evaluate within the data frame or list context.
Example:
# Create a data frame
df <- data.frame(name = c("John", "Jane", "Jim"),
age = c(25, 30, 28),
salary = c(50000, 60000, 55000))
(ii) within()
Theory: The within() func on allows modifica on of a data frame or list by evalua ng an
expression within its context. Unlike with(), which only evaluates the expression, within()
also allows you to modify or add new columns to the data frame.
Syntax:
within(data, expression)
data: The data frame or list.
expression: The R expression to evaluate, which may include changes or addi ons to
columns.
Example:
# Create a data frame
df <- data.frame(name = c("John", "Jane", "Jim"),
age = c(25, 30, 28),
salary = c(50000, 60000, 55000))
# Use within() to add a new column
df <- within(df, {
salary_increase <- salary * 1.1 # Increase salary by 10%
})
print(df)
Explana on:
within() allows us to add the salary_increase column to df, which contains the
increased salaries by 10%. The modifica on is performed within the context of the
data frame.
(iii) order()
Theory: The order() func on returns the indices that would sort a vector or column of a data
frame in a specific order (ascending or descending). It is commonly used for sor ng data
frames based on one or more columns.
Syntax:
order(x, decreasing = FALSE, na.last = TRUE)
x: The vector or column to sort.
decreasing: Logical value indica ng if sor ng should be in descending order (TRUE) or
ascending (FALSE).
na.last: Controls whether NA values should be sorted at the beginning or the end.
Example:
# Create a data frame
df <- data.frame(name = c("John", "Jane", "Jim"),
age = c(25, 30, 28),
salary = c(50000, 60000, 55000))
(i) apply()
The apply() func on is used to apply a func on to the rows or columns of a matrix or array. It
takes three main arguments: the data (matrix/array), the margin (1 for rows, 2 for columns),
and the func on to apply.
Syntax:
apply(X, MARGIN, FUN, ...)
X: The matrix or array.
MARGIN: The margin to apply the func on to. 1 for rows, 2 for columns.
FUN: The func on to apply.
Example:
# Create a matrix
mat <- matrix(1:12, nrow = 3, ncol = 4)
print("Original Matrix:")
print(mat)
(iii) mapply()
The mapply() func on applies a func on to mul ple arguments or lists in parallel. It is a
mul variate version of sapply().
Syntax:
mapply(FUN, ..., MoreArgs = NULL)
FUN: The func on to apply.
...: The arguments or lists to apply the func on to.
Example:
# Two lists
list1 <- c(1, 2, 3)
list2 <- c(4, 5, 6)
(iv) rapply()
The rapply() func on applies a func on recursively to each element of a list or vector,
including nested lists, and simplifies the result if possible.
Syntax:
rapply(X, FUN, classes = "ANY", ...)
X: The list to apply the func on to.
FUN: The func on to apply.
classes: The class of the elements to apply the func on to (default is "ANY").
Example:
# Nested list
nested_list <- list(a = list(1:3), b = list(4:6), c = list(7:9))
(v) tapply()
The tapply() func on applies a func on to subsets of a vector, which are defined by a factor
or grouping variable. It is commonly used for grouped analysis.
Syntax:
tapply(X, INDEX, FUN, ...)
X: The vector to apply the func on to.
INDEX: A factor or list of factors that define the grouping.
FUN: The func on to apply.
Example:
# Create a vector and a factor for grouping
values <- c(10, 20, 30, 40, 50)
groups <- factor(c("A", "A", "B", "B", "A"))
5. Develop R code to demonstrate the concept of data reshaping using cbind() and rbind()
func on with relevant input and output.
ANS:
5. Data Reshaping using cbind() and rbind() in R
In R, cbind() (column bind) and rbind() (row bind) are func ons used to combine data. These
func ons are commonly used for reshaping data by adding columns or rows to an exis ng
data frame or matrix.
Here is a demonstra on of both func ons with relevant input and output.
Matrix 2:
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
Matrix 2:
[,1] [,2]
[1,] 5 7
[2,] 6 8
# Using rbind to combine data frames by rows (ensure the column names match)
df3 <- data.frame(ID = 4, Name = "David", Age = 40, City = "Miami")
combined_df_row <- rbind(df1, df3)
cat("\nCombined Data Frame (by Rows):\n")
print(combined_df_row)
Output:
Data Frame 1:
ID Name
1 1 Alice
2 2 Bob
3 3 Charlie
Data Frame 2:
Age City
1 25 New York
2 30 Los Angeles
3 35 Chicago
2. sub()
sub() is used to replace the first occurrence of a pa ern in a string with a replacement string.
Syntax:
sub(pa ern, replacement, x)
pa ern: The pa ern to match.
replacement: The string to replace the pa ern.
x: The character vector to search within.
Example:
sub("my", "your", "This is my pen")
Explana on:
The func on searches for the first occurrence of the substring "my" and replaces it
with "your".
Here, the first occurrence of "my" in "This is my pen" will be replaced by "your",
resul ng in the string "This is your pen".
Output:
[1] "This is your pen"
MODULE-4
1. Write a note on the main graphical Packages in R.
Ans:
1. Base R Graphics
Base R comes with built-in func ons for crea ng a wide range of standard plots. It is
simple to use and doesn’t require any addi onal packages for basic visualiza ons.
Features:
Basic Plo ng Func ons: Func ons like plot(), hist(), boxplot(), barplot(), pie(),
sca erplot(), and lines() can create basic plots.
Customiza on: The plots can be customized with par(), tle(), and axis() func ons.
Layering: Mul ple graphical elements like points, lines, and text can be layered on
the same plot using addi onal commands.
Example:
# Base R plot example
plot(1:10, type = "o", col = "blue", xlab = "X-Axis", ylab = "Y-Axis")
While Base R graphics are useful for quick and simple visualiza ons, it lacks the flexibility
and advanced features required for more complex and interac ve visualiza ons.
3. la ce
la ce is another powerful package for crea ng high-level data visualiza ons, designed
to create plots in a consistent and customizable way.
Features:
Trellis Plots: la ce is well-known for its support for trellis (or small mul ple) plots,
which allow you to display mul ple similar plots in a grid layout.
Paneling: The grid system in la ce allows for condi onal plo ng, where data is split
based on a factor variable.
Reproducibility: The syntax is consistent across different plot types, making it easy to
apply across mul ple plot layouts.
Example:
# la ce example
library(la ce)
xyplot(hwy ~ displ | class, data = mpg, main = "Engine Displacement vs. Highway MPG by
Class")
la ce is preferred for mul -panel plots and working with condi oned plots, but it may
require more effort in customiza on compared to ggplot2.
4. plotly
plotly is a package used to create interac ve and dynamic plots. It is especially useful for
web applica ons and interac ve data explora on.
Features:
Interac ve Plots: You can hover, zoom, and click to explore the data.
Integra on with ggplot2: It can convert ggplot2 plots to interac ve plots with
minimal code.
Web-Ready Visualiza ons: It integrates seamlessly with web technologies, making it
a great op on for dashboards and web applica ons.
Example:
# plotly example
library(plotly)
p <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
geom_point()
ggplotly(p)
plotly is excellent for crea ng interac ve, web-based visualiza ons and is commonly
used in data-driven web applica ons.
5. shiny
shiny is an R package for building interac ve web applica ons. Although it is primarily
used for interac ve applica ons, it also supports embedding various types of plots and
charts.
Features:
Web Applica ons: Create complete web applica ons with interac vity, including
dynamic user interfaces and reac ve plots.
Integra on with Plo ng Packages: It can display interac ve plots created with
ggplot2, plotly, and other libraries.
Custom UI Elements: Includes custom input controls (sliders, bu ons, drop-downs)
to control the visualiza on.
Example:
library(shiny)
# Define UI
ui <- fluidPage(
sliderInput("slider", "Select a value", min = 1, max = 10, value = 5),
plotOutput("plot")
)
# Define server
server <- func on(input, output) {
output$plot <- renderPlot({
plot(1:input$slider, 1:input$slider)
})
}
6. highcharter
highcharter is an R wrapper for the Highcharts JavaScript library, allowing the crea on of
interac ve charts and visualiza ons.
Features:
Interac ve Charts: highcharter creates interac ve visualiza ons that can be easily
integrated into web applica ons.
Advanced Chart Types: Supports charts such as line charts, bar charts, pie charts,
and more advanced charts like heatmaps, radar charts, and tree maps.
Example:
library(highcharter)
highchart() %>%
hc_chart(type = "line") %>%
hc_add_series(data = c(1, 3, 2, 4, 5), name = "Example Line Chart")
highcharter is ideal for crea ng interac ve and aesthe cally pleasing charts, especially
when building dashboards and web apps.
2. Write the basic syntax for crea ng pie chart and explain each parameter listed in
the syntax. Also write a R program to create a pie chart for the given list of flowers
with count [Rose=25, Lotus=35, Lilly=10, Sunflower=5, Jasmine=15]. Draw the
created output chart. (10)
ANS:
Basic Syntax for Crea ng a Pie Chart in R
The basic syntax for crea ng a pie chart in R is:
pie(x, labels = NULL, edges = 200, radius = 0.8, col = NULL,
main = NULL, border = NULL, clockwise = FALSE, density = NULL,
angle = 45, init.angle = 90, ...)
Explana on of Parameters:
x: A vector of numeric values represen ng the data to be plo ed in the pie chart.
These values are typically the counts or propor ons of categories.
labels: A vector of labels corresponding to the segments of the pie chart. If not
provided, R will label the segments as "1", "2", "3", etc.
edges: The number of edges used to create the pie chart. A higher value gives a
smoother pie chart.
radius: The radius of the pie chart. The default value is 0.8. Increasing the value
makes the pie chart larger.
col: A vector of colors to fill the segments of the pie chart. If not specified, R will use
default colors.
main: The main tle of the pie chart.
border: The color of the border surrounding the segments. If NULL, no border is
drawn.
clockwise: A logical value (TRUE or FALSE). If TRUE, the chart will be drawn clockwise;
if FALSE, it will be counterclockwise.
density: The density of shading lines for the pie chart segments. If NULL, no shading
is used.
angle: The angle of the first slice in the pie chart. The default value is 45 degrees.
init.angle: The angle where the first slice is drawn (default is 90 degrees).
...: Addi onal arguments for customiza on, such as cex for text size or font for font
type.
# Plo ng
plot(male, type = "o", col = "blue", xlab = "Data Points", ylab = "Wages",
main = "Wages by Category", ylim = range(0, max(male, female, child)))
lines(female, type = "o", col = "red")
lines(child, type = "o", col = "green")
# Adding a legend
legend("topright", legend = wages, col = c("blue", "red", "green"), lty = 1, pch = 1)
4. Write the syntax for plo ng the histogram and plot the histogram for the following
data. x<-c(45,33,31,23,58,47,39,58,28,55,42,27)
Ans:
Syntax for Plo ng a Histogram in R
The basic syntax to create a histogram in R is:
hist(x,
breaks = NULL, # Number of bins or breakpoints (op onal)
col = NULL, # Color of the bars (op onal)
main = NULL, # Main tle (op onal)
xlab = NULL, # Label for the x-axis (op onal)
ylab = NULL, # Label for the y-axis (op onal)
border = NULL, # Color of the borders (op onal)
xlim = NULL, # Limits for the x-axis (op onal)
ylim = NULL, # Limits for the y-axis (op onal)
...)
x: A numeric vector of data values.
breaks: Defines the number of bins or specific breakpoints. If NULL, R will
automa cally calculate the number of bins.
col: Color of the bars in the histogram.
main: The tle of the histogram.
xlab: Label for the x-axis.
ylab: Label for the y-axis.
border: Color for the borders of the bars.
xlim and ylim: Control the limits of the x-axis and y-axis.
A line chart/line plot is a graph that connects a series of points by drawing line
segments between them.
The plot() func on in R is used to create the line graph in base graphics as in fig.
This func on takes a vector of numbers as input together with few more parameter
listed below.
y Numeric vector
type takes the value ”p”(only points) or “l”(only lines) or ”o”(both lines and points)
> male<-c(1000,2000,1500,4000,800)
> female<-c(700,300,600,1200,800)
> child<-c(1000,1200,1500,800,2000)
> wages<-c("Male","Female","Children")
> color=c("red","blue","green")
> plot(male,type="o",col="red",xlab="Month",ylab="Wages",main="Monthly
wages",ylim=c(0,5000))
> lines(female,type="o",col="blue")
> lines(child,type="o",col="green")
Histograms
Histogram represents the variable values frequencies, that are split into ranges. This
is similar to bar charts but histograms group values into con nuous ranges. In R histograms
in base graphics are drawn using the func on hist() as in figure, that takes a vector of
numbers as input together with few more parameters listed below.
V numeric vector
Example
x<-c(45,33,31,23,58,47,39,58,28,55,42,27)
hist(x,xlab=”Age”,col=”blue”,border=”red”,xlim=c(25,60),ylim=c(0,3),breaks=5)
6. Let us use the built-in dataset air quality which has Daily air quality measurements in
New York, May to September 1973. Develop R program to generate histogram by using
appropriate arguments for the following statements.
a) Assigning names, using the air quality data set.
b) Change colors of the Histogram
c) Remove Axis and Add labels to Histogram
d) Change Axis limits of a Histogram
e) Add Density curve to the histogram.
ANS:
To work with the airquality dataset in R and create a histogram with various customiza ons,
let's break down each part of the task:
a) Assigning names using the airquality dataset
The airquality dataset contains daily air quality measurements in New York from May to
September in 1973. It has several variables like Ozone, Solar.R, Wind, Temp, and Month.
We will use the Ozone variable for the histogram in this example, and we will assign the
column names to the dataset.
b) Change colors of the Histogram
We can specify the color of the bars in the histogram using the col argument.
c) Remove Axis and Add labels to Histogram
We can remove axis labels using the xaxt and yaxt arguments, and then manually add labels
using the mtext() func on.
d) Change Axis limits of a Histogram
To change the axis limits, use the xlim and ylim arguments.
e) Add a Density curve to the Histogram
To add a density curve over the histogram, we can use the lines() func on to add a density
curve.
R Code:
# Load the airquality dataset
data(airquality)
# a) Assigning names to the dataset (already assigned, but we can reassign or modify)
names(airquality) <- c("Ozone", "Solar.R", "Wind", "Temp", "Month", "Day")
head(airquality)
9.Plot the bar plot for following data, both horizontal and ver cal. x<-
matrix(c(1000,900,1500,4400,800,2100,1700,2900,3800), nrow=3,ncol=3) years<-
c(“2011”,”2012”,”2013”) city<-c(“Chennai”,”Mumbai”,”Kolkata”)
Ans:
R Code for Ver cal and Horizontal Bar Plots:
# Create matrix data
x <- matrix(c(1000, 900, 1500, 4400, 800, 2100, 1700, 2900, 3800), nrow=3, ncol=3)
MODULE-5