0% found this document useful (0 votes)
11 views4 pages

DS Exp4

The document outlines an experiment focused on data exploration, manipulation, visualization, and importing in R using a retail company's transaction dataset. It details a procedure that includes importing data, filtering transactions, summarizing statistics, creating visualizations, and saving modified data to a new CSV file. The provided R code demonstrates the implementation of these tasks effectively.

Uploaded by

LIGHTNING BOLT
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)
11 views4 pages

DS Exp4

The document outlines an experiment focused on data exploration, manipulation, visualization, and importing in R using a retail company's transaction dataset. It details a procedure that includes importing data, filtering transactions, summarizing statistics, creating visualizations, and saving modified data to a new CSV file. The provided R code demonstrates the implementation of these tasks effectively.

Uploaded by

LIGHTNING BOLT
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/ 4

EXP.

NO:4
Data exploration, manipulation,visualization
DATE: and importing in R
Aim:
To study about data manipulations, exploration,visualization and
importing in R.

Question:

A retail company has a dataset containing customer transactions with columns


for Customer_ID, Product, Quantity, Price, and Transaction_Date. Your tasks
include reading the dataset from a CSV file, filtering the data to show
transactions where the Quantity is greater than 5, summarizing basic statistics
for Price and Quantity, creating a bar plot to visualize the number of
transactions per product, calculating the total purchase amount for each
customer, and saving the modified dataset with an additional column for total
purchase amount to a new CSV file. Write R code to perform these operations
efficiently.

Procedure:

Step 1 : Import the CSV file.


Step 2 : Filter transactions with Quantity > 5.
Step 3 : Compute summary statistics for Price and Quantity.
Step 4 : Create a bar plot for transactions per product.
Step 5 : Calculate the total purchase amount for each customer.
Step 6 : Save the modified dataset with the total purchase amount to a new CSV
file.

Source code:

getwd()
setwd("C:/Users/Darshana/OneDrive/Desktop") # Replace with the actual path
# Load necessary libraries
library(dplyr)
library(ggplot2)

# Read the dataset from a CSV file


data <- read.csv("transactions.csv", stringsAsFactors = FALSE)

# Convert the Transaction_Date column to Date format


data$Transaction_Date <- as.Date(data$Transaction_Date, format = "%Y-%m-

ARJUN SUDHEER (71812201021)


%d")

# Filter the data to show transactions where Quantity > 5


filtered_data <- data %>% filter(Quantity > 5)

# Summarize basic statistics for Price and Quantity


summary_stats <- filtered_data %>% summarise(
mean_price = mean(Price, na.rm = TRUE),
median_price = median(Price, na.rm = TRUE),
sd_price = sd(Price, na.rm = TRUE),
mean_quantity = mean(Quantity, na.rm = TRUE),
median_quantity = median(Quantity, na.rm = TRUE),
sd_quantity = sd(Quantity, na.rm = TRUE)
)

# Print summary statistics


print(summary_stats)

# Create a bar plot to visualize the number of transactions per product


ggplot(filtered_data, aes(x = Product)) +
geom_bar(fill = "blue") +
labs(title = "Number of Transactions per Product", x = "Product", y = "Number
of Transactions") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))

# Calculate the total purchase amount for each transaction


filtered_data <- filtered_data %>%
mutate(Total_Purchase_Amount = Quantity * Price)

# Calculate the total purchase amount for each customer (aggregate)


total_purchase_per_customer <- filtered_data %>%
group_by(Customer_ID) %>%
summarise(Total_Amount_Spent = sum(Total_Purchase_Amount, na.rm =
TRUE))

# Merge the total purchase amount back into the dataset


filtered_data <- left_join(filtered_data, total_purchase_per_customer, by =
"Customer_ID")

# Save the modified dataset to a new CSV file


write.csv(filtered_data, "modified_transactions.csv", row.names = FALSE)

ARJUN SUDHEER (71812201021)


Output:

ARJUN SUDHEER (71812201021)


Result:
Thus data explorations, manipulations , visualization and importing in R
was studied and executed successfully.

ARJUN SUDHEER (71812201021)

You might also like