Functions Vs Scripts and Datasets
Functions Vs Scripts and Datasets
1. Code Reusability: Functions can be reused in multiple parts of your program, reducing
redundancy and saving time.
2. Modularity: Functions allow you to break your code into smaller, manageable chunks, making it
easier to read and debug.
3. Ease of Maintenance: When code is organized into functions, making changes or fixing issues
becomes simpler without affecting unrelated parts of the code.
4. Parameterization: Functions can accept arguments, allowing you to handle different inputs
dynamically.
5. Improved Readability: Functions encapsulate specific tasks, making the overall program structure
cleaner and more understandable.
To combine datasets in R, you can use the `c()` function, which concatenates values into a single vector.
Example
Given:
Dataset A: `6, 7, 8, 9`
Dataset B: `1, 2, 4, 5`
# Define datasets
A <- c(6, 7, 8, 9)
B <- c(1, 2, 4, 5)
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/25
# Combine datasets
C <- c(A, B)
Output:
csharp
[1] 6 7 8 9 1 2 4 5
Q) List and explain various functions that allow users to handle data in R workspace with
appropriate examples.
In R, the workspace is where all your objects, such as variables, data frames, vectors, and functions,
reside during your session. Below is a list of essential functions to handle data in the R workspace, along
with explanations and examples:
1. `ls()`
x <- 5
y <- c(1, 2, 3)
ls()
Output:
csharp
2. `rm()`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/25
R
Output:
csharp
[1] "y"
3. `rm(list = ls())`
rm(list = ls())
ls()
Output:
scss
character(0)
4. `get()`
z <- 10
get("z")
Output:
csharp
[1] 10
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/25
5. `assign()`
assign("new_var", 42)
print(new_var)
Output:
csharp
[1] 42
6. `exists()`
exists("new_var")
Output:
csharp
[1] TRUE
7. `save()`
8. `load()`
Description: Loads objects from a saved file back into the workspace.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/25
Example:
load("my_data.RData")
print(y)
9. `save.image()`
save.image(file = "workspace.RData")
10. `history()`
history()
11. `gc()`
gc()
12. `objects()`
objects()
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/25
13. `str()`
str(y)
Output:
css
num [1:3] 1 2 3
14. `summary()`
summary(y)
Output:
mathematica
15. `dir()`
dir()
These functions allow users to efficiently manage, organize, and retrieve data in the R workspace.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/25
Q) Consider the following data frame given below: course id class 1 11 marks 1 2 12 56 2 3 13 75 1 4
14 48 2 5 15 69 1 6 16 84 2 53 i.Create a subset of course less than 3 by using [ ] brackets and
demonstrate the output. ii. Create a subset where the course column is less than 3 or the class
equals to 2 by using subset () function and demonstrate the output.
Q) List and discuss various types of data structures in R.
df <- data.frame(
course = c(1, 2, 3, 4, 5, 6),
id = c(11, 12, 13, 14, 15, 16),
class = c(1, 2, 1, 2, 1, 2),
marks = c(53, 56, 75, 48, 69, 84)
)
Output:
kotlin
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/25
Output:
kotlin
R supports several data structures, each suited for different types of data. Here’s a discussion of the
main types:
1. Vectors
Description: A one-dimensional data structure that holds elements of the same type (numeric,
character, logical, etc.).
Example:
Output:
csharp
[1] 1 2 3 4
2. Matrices
Description: Two-dimensional arrays where all elements must be of the same type.
Example:
Output:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/25
css
3. Arrays
Description: Multi-dimensional data structures (more than 2 dimensions) with elements of the
same type.
Example:
Output:
css
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
4. Data Frames
Description: Two-dimensional structures that can hold data of different types in columns.
Example:
Output:
css
name age
1 A 25
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/25
2 B 30
5. Lists
Description: A versatile data structure that can hold elements of different types, including other
lists.
Example:
Output:
bash
$name
[1] "John"
$age
[1] 25
$marks
[1] 85 90
6. Factors
Output:
csharp
7. Tibble
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/25
Example (requires `dplyr`):
library(tibble)
tib <- tibble(name = c("A", "B"), age = c(25, 30))
print(tib)
Output:
less
# A tibble: 2 × 2
name age
<chr> <dbl>
1 A 25
2 B 30
8. NULL
x <- NULL
print(x)
Output:
arduino
NULL
These data structures provide flexibility in data handling and are foundational to programming in R.
i. The following table shows the number of units of different products sold on
different days:
Product
Monday Tuesday Wednesday Thursda
y
Bread
12
3
5
Friday
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/25
Milk
11
21
27
18
9
Cola Cans
20
10
1
33
6
15
Chocolate
bars
12
6
7
4
13
Detergent
5
8
12
12
Create five sample numeric vectors from this data.
20
23
ii. Name and explain the operators used to form data subsets in R.
Milk 11 21 27 18 9
Cola Cans 20 10 1 33 6
Chocolate Bars 12 6 7 4 13
Detergent 5 8 12 12 15
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/25
R
# Bread sales
bread <- c(12, 3, 5, 20, 23)
# Milk sales
milk <- c(11, 21, 27, 18, 9)
# Detergent sales
detergent <- c(5, 8, 12, 12, 15)
Output:
csharp
[1] 12 3 5 20 23
[1] 11 21 27 18 9
[1] 20 10 1 33 6
[1] 12 6 7 4 13
[1] 5 8 12 12 15
R provides several operators for subsetting data. These can be applied to vectors, matrices, data frames,
and lists.
1. Indexing by Position
Use square brackets `[ ]` to specify the position of the elements you want to extract.
Example:
Output:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 13/25
csharp
[1] 20
Output:
csharp
[1] 30 40
3. Indexing by Names
Output:
csharp
[1] 20
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 14/25
R
Output:
product sales
2 Milk 15
Output:
csharp
[1] 12 15
6. Indexing Matrices
Output:
csharp
[1] 4
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 15/25
7. Using `%in%`
Output:
csharp
[1] 20 30
8. Negative Indexing
Output:
csharp
[1] 10 30 40
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 16/25
The provided table includes data for the number of units of various products sold on different days.
Here's how to address the questions:
From the table, each row corresponds to a product's sales over the days of the week. We can create a
numeric vector for each product:
Output:
csharp
[1] 12 3 5 11 9
[1] 21 27 18 20 15
[1] 10 1 33 6 15
[1] 6 7 4 13 12
[1] 5 8 12 20 23
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 17/25
1. `[ ]` Brackets
Output:
csharp
[1] 1 2 3
2. `$` Operator
Output:
csharp
[1] 12 21
3. `subset()` Function
Output:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 18/25
product sales
2 Milk 21
4. Logical Indexing
Output:
csharp
[1] 21 18
5. Named Indexing
Output:
mathematica
Tuesday
21
6. Negative Indexing
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 19/25
R
Output:
csharp
[1] 2 4 5
7. `%in%` Operator
Output:
csharp
8. Multiple Conditions
Output:
csharp
[1] 18
These operators provide robust and flexible options for filtering and extracting data in R.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 20/25
Q) Describe applications of data visualization.
Q) Consider the following data frame given below:
course
id
class
1
11
marks
1
2
12
56
2
3
13
75
1
4
14
48
2
5
15
69
1
6
16
84
2
i.
53
Create a subset of course less than 5 by using [ ] brackets and demonstrate
the output.
ii.
Create a subset where the course column is less than 4 or the class equals
to 1 by using subset () function and demonstrate the output.
Q)i. Write a script to create a dataset named data1 in R containing the following text.
Text: 2, 3, 4, 5, 6.7, 7, 8.1, 9
ii. Explain the various functions provided by R to combine different sets of data.
Data visualization has widespread applications across various domains. Some key applications include:
1. Business Intelligence
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 21/25
Helps companies analyze sales trends, customer behavior, and financial performance.
Example: Dashboards displaying monthly sales performance.
2. Scientific Research
3. Healthcare Analytics
4. Education
Data Frame
Given data:
3 13 75 1
4 14 48 2
5 15 69 1
6 16 84 2
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 22/25
R
Output:
kotlin
Output:
kotlin
i) Creating a Dataset
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 23/25
R
Output:
csharp
1. `c()`:
Combines elements into a vector.
Example:
2. `rbind()`:
Combines datasets by rows.
Example:
3. `cbind()`:
Combines datasets by columns.
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 24/25
4. `merge()`:
Merges two data frames based on common columns or rows.
Example:
5. `list()`:
Combines objects into a list.
Example:
Each function has specific use cases depending on the structure and requirement of the data.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 25/25