R Questions Solution (BDA) : Subscribe
R Questions Solution (BDA) : Subscribe
com/@ataglanceofficial
1.
Solution:
(i)
# Create a sample data frame with the given marks values
data <- data.frame(
subject = c(1, 2, 3, 4, 5, 6),
class = c(1, 2, 1, 2, 1, 2),
marks = c(56, 75, 48, 69, 84, 53)
)
Output:
This code creates a data frame with the specified marks values and then creates a
subset where the "subject" is less than 4.
(ii)
# Create a sample data frame with the given marks values
data <- data.frame(
subject = c(1, 2, 3, 4, 5, 6),
class = c(1, 2, 1, 2, 1, 2),
marks = c(56, 75, 48, 69, 84, 53)
)
Output:
In this example, data$subject < 3 checks if the "subject" is less than 3, and
data$class == 2 checks if the "class" is equal to 2. The resulting subset includes
rows where both conditions are true. The subset is created using square brackets
[].
Subscribe: https://youtube.com/@ataglanceofficial
2.
Solution:
(i)
# Create a data frame for the given sales data
sales_data <- data.frame(
product = c("bread", "milk", "cola cans", "chocolate bars",
"detergent"),
monday = c(12, 21, 10, 6, 5),
tuesday = c(3, 27, 1, 7, 8),
wednesday = c(5, 18, 33, 4, 12),
thursday = c(11, 20, 6, 13, 20),
friday = c(9, 15, 12, 12, 23)
)
Output:
In the given sales data table, each row represents a different product, and each
column from Monday to Friday represents the number of units sold for that
product on each respective day. The sample numeric vectors are randomly selected
columns from this table, representing the sales data for a particular day across all
products. These vectors can be used for further analysis, such as calculating daily
averages or comparing the sales performance of different products on a specific
day.
Subscribe: https://youtube.com/@ataglanceofficial
In R, the primary operators and functions for forming data subsets include:
These operators and functions offer flexibility in manipulating and analyzing data,
allowing users to efficiently subset datasets based on various conditions or
criteria.
Subscribe: https://youtube.com/@ataglanceofficial
3.
Solution:
(i)
# Create a data frame with the given records
employee_data <- data.frame(
sr_number = 1:10,
name = c("Vivek", "Karan", "James", "Soham", "Renu", "Farah",
"Hetal", "Mary", "Ganesh", "Krish")
)
print("Employee Dataset:")
print(employee_data)
Output:
(ii)
# Create a data frame with the salaries of 5 new employees
new_employees <- data.frame(
sr_number = 11:15,
name = c("Amit", "Neha", "Rahul", "Sara", "Rohit"),
salary = c(60000, 45000, 58000, 52000, 48000)
)
In this demonstration, the rbind() function is used to combine the existing dataset
(employee_data) with the data frame of salaries for 5 new employees
(new_employees). The result is a combined dataset (combined_data) with the
salaries of all 15 employees.
Output:
Subscribe: https://youtube.com/@ataglanceofficial
4.
Solution:
(i)
# Creating the data frame with the given information
course_data <- 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(56, 75, 48, 69, 84, 53)
)
# Subset using []
subset_course_less_than_3 <- course_data[course_data$course < 3,
]
Output:
Used square brackets ([]) to select rows where the "course" column is less than 3.
(ii)
# Subset using subset()
subset_course_class_condition <- subset(course_data, course < 3
| class == 2)
Used the subset() function to select rows where either the "course" column is less
than 3 or the "class" column equals 2.