Write a function in R, called myDiamondManipulatorO that has a primary input arg
ID: 3743311 • Letter: W
Question
Write a function in R, called myDiamondManipulatorO that has a primary input argument reserved for the dia- monds dataset and does the following: (a) Has a default input arguments cut "Premium", and color NULL. Note the colors are specified by "D" ..E'', ''F', “G,,, ..H',' ..!", "J", otherwise. It should be able to handle reading in individual values or character arrays such as cut cFair". "Very Good"). color c(E" "H". "J"), etc. (b) Determines the second highest price for the color specified in the input. If no color is specified, then return the second highest price for all of the colors in the dataset. (c) Determines the average price for each combination of cut and color specified in the input arguments. If no color is specified, then return the average price for every combination of cut and color in the dataset (d) Determines today's date. (e) Plots the price versus carat for the specified cut diamonds. (f) Returns the objects from (b-d), in outputs: pricein2, meanPriceByColor, studyDate.Explanation / Answer
###Headers##
#For drawing graphs##
library(ggplot2)
#For data manipulation##
library(dplyr)
##Load the dataset##
data("diamonds")
###d) Today's date##
studyDate<-Sys.Date()
###e) Price versus carat##
###For specified diamonds you will need to subset the diamonds dataset
###This subsetting can be done by filter or subset command
##Draw histogram between price and carat
##Label the graph accordingly
ggplot(data=diamonds) + geom_histogram(binwidth=50, aes(x=diamonds$price/diamonds$carat)) + ggtitle("Diamond Price per Carat Distribution by Cut") +xlab("Diamond Price per Carat U$") + ylab("Frequency") + theme_minimal() + facet_wrap(~cut)
### f ###
###Second highest price###
####Take out the two highest prices for each color##
###First group by color and then select the price##
##Take out the top two price for each color##
diamonds1<-diamonds %>% group_by(color)%>%select(price) %>%
top_n(2)%>% arrange((price),.by_group=TRUE)
###Take out the lowest price from this group of 2 prices for each color##
pricein2<-diamonds1 %>% group_by(color)%>% top_n(1)
#### Mean price by cut and color##
##Group the data by cut and color##
##After that select the average price##
meanPriceBycolor<-diamonds %>% group_by(cut,color) %>% summarise(avg_price=mean(price))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.