library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Load the diamonds dataset
data(diamonds)
# Subset the data to include at least 9 columns (adjust as needed)
selected_columns <- diamonds[, c("carat", "depth", "table", "price", "x", "y", "z", "cut", "color", "clarity")]
# Group by 'cut' and summarize 'price'
grouped_by_cut <- diamonds %>%
group_by(cut) %>%
summarize(avg_price = mean(price),
min_price = min(price),
max_price = max(price))
# Print the results
print(grouped_by_cut)
## # A tibble: 5 × 4
## cut avg_price min_price max_price
## <ord> <dbl> <int> <int>
## 1 Fair 4359. 337 18574
## 2 Good 3929. 327 18788
## 3 Very Good 3982. 336 18818
## 4 Premium 4584. 326 18823
## 5 Ideal 3458. 326 18806
# Group by 'color' and summarize 'carat'
grouped_by_color <- diamonds %>%
group_by(color) %>%
summarize(avg_carat = mean(carat),
min_carat = min(carat),
max_carat = max(carat))
# Print the results
print(grouped_by_color)
## # A tibble: 7 × 4
## color avg_carat min_carat max_carat
## <ord> <dbl> <dbl> <dbl>
## 1 D 0.658 0.2 3.4
## 2 E 0.658 0.2 3.05
## 3 F 0.737 0.2 3.01
## 4 G 0.771 0.23 3.01
## 5 H 0.912 0.23 4.13
## 6 I 1.03 0.23 4.01
## 7 J 1.16 0.23 5.01
# Group by both 'cut' and 'color' and summarize 'price'
grouped_by_cut_and_color <- diamonds %>%
group_by(cut, color) %>%
summarize(avg_price = mean(price),
min_price = min(price),
max_price = max(price))
## `summarise()` has grouped output by 'cut'. You can override using the `.groups`
## argument.
# Print the results
print(grouped_by_cut_and_color)
## # A tibble: 35 × 5
## # Groups: cut [5]
## cut color avg_price min_price max_price
## <ord> <ord> <dbl> <int> <int>
## 1 Fair D 4291. 536 16386
## 2 Fair E 3682. 337 15584
## 3 Fair F 3827. 496 17995
## 4 Fair G 4239. 369 18574
## 5 Fair H 5136. 659 18565
## 6 Fair I 4685. 735 18242
## 7 Fair J 4976. 416 18531
## 8 Good D 3405. 361 18468
## 9 Good E 3424. 327 18236
## 10 Good F 3496. 357 18686
## # ℹ 25 more rows
Task 1: Grouping by Cut and Summarizing Price
Insights: We found that the “Ideal” cut has the highest average price, while the “Fair” cut has the lowest. Price varies significantly with the cut.
Significance: Cut quality is a crucial determinant of diamond price, making this insight important for buyers.
Further Questions: What factors drive price differences between cuts? Are there correlations between cut and other diamond characteristics?
Task 2: Grouping by Color and Summarizing Carat
Insights: Lower color grades have larger average carat sizes, while higher grades have smaller sizes.
Significance: Color plays a role in pricing, and customers seeking specific carat sizes should consider color grades.
Further Questions: How does color-carat size affect diamond appearance and value? Are there patterns in color grade distribution across carat size ranges?
Task 3: Grouping by Cut and Color and Summarizing Price
Insights: Average prices vary not only with cut but also with color. Ideal cut diamonds generally have the highest prices across all colors.
Significance: This analysis helps consumers and retailers price diamonds based on cut and color combinations.
Further Questions: Are specific cut and color combinations consistently associated with higher or lower prices? How does combination affect diamond appearance and perceived quality?
# Group the data by 'cut' and count the number of rows in each group
grouped_data <- diamonds %>%
group_by(cut) %>%
mutate(group_count = n())
# Find the group(s) with the lowest count(s)
min_count <- min(grouped_data$group_count)
# Assign a special tag to the groups with the lowest count(s)
grouped_data <- grouped_data %>%
mutate(special_tag = ifelse(group_count == min_count, "Lowest Probability", "Normal"))
# Now you can visualize the data with special tags
# For example, a box plot of price by special tag
ggplot(grouped_data, aes(x = special_tag, y = price, fill = special_tag)) +
geom_boxplot() +
labs(title = "Price Distribution by Special Tag", x = "Special Tag", y = "Price")
Task: Identifying and Tagging Underrepresented Groups
Insights: We’ve identified and tagged the groups with the lowest probability of being selected based on the number of rows in each group.
Significance: This allows us to focus on underrepresented categories in our analysis, which can be valuable for uncovering unique insights or addressing potential biases in the data.
Further Questions:
What specific characteristics distinguish the underrepresented groups from the others? Are there any patterns or anomalies in the data within these underrepresented categories? How might these underrepresented categories impact decision-making or analysis results?
# Create a summary table with counts of each combination
combination_counts <- diamonds %>%
group_by(cut, color) %>%
summarize(count = n())
## `summarise()` has grouped output by 'cut'. You can override using the `.groups`
## argument.
# Create a heatmap
ggplot(combination_counts, aes(x = cut, y = color, fill = count)) +
geom_tile() +
scale_fill_gradient(low = "white", high = "blue") +
labs(title = "Frequency of Cut and Color Combinations",
x = "Cut", y = "Color", fill = "Count")
Task: Identifying Common and Rare Combinations of “Cut” and “Color” in Diamonds Dataset
Insights: Some combinations of “cut” and “color” may never show up due to rarity, while others are more common. For example, “Ideal” cut with “G” color is common, while “Fair” cut with “D” color is rare.
Significance: Understanding the rarity and commonality of these combinations can help in pricing strategies, inventory management, and catering to consumer preferences.
Further Questions:
Why are certain combinations rare, and what factors contribute to their scarcity? How do these combinations correlate with diamond prices or customer choices? Can marketing strategies be adjusted to promote less common combinations?