Im doing R programming question on a dataset called Diamonds. First of all in order to install and load the dataset. Type the following command
install.packages("ggplot2")
library(ggplot2)
diamonds
Now because so many values and names, I need to find out the 7 most expensive prices for diamonds (dataset) of Ideal cut?
What i did is, I created a dataframe called diamond.ideal and inside the dataframe I put 3 columns and values from dataset diamonds. here's the code
diamond.ideal <- data.frame(diamonds$cut,diamonds$color, diamonds$price)
head(diamond.ideal) #or diamond.ideal
Here's the screenshot of the output
Finally, I need to find the 7 most expensive prices for diamonds of Ideal cut? This is my code But not sure it's right
diamond.ideal[which(diamond.ideal$diamonds.cut == "Ideal", diamond.ideal$diamonds.price == max(diamond.cut$diamonds.price))[990:997],]
[990:997] are the row numbers (I think) Here's the screenshot
I don't know if the figures are right, there are so many values on the dataset. I just want to know if that highest values are correct? or is there another way to find he 7 most expensive prices for diamonds of Ideal by using different function like table() or cut() or others?
There are several ways of doing this. Here's one taking your approach.
library(ggplot2)
data(diamonds)
xy <- diamonds[diamonds$cut == "Ideal", ]
> xy[order(xy$price, decreasing = TRUE), ][1:7, ]
# A tibble: 7 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 1.51 Ideal G IF 61.7 55 18806 7.37 7.41 4.56
2 2.07 Ideal G SI2 62.5 55 18804 8.20 8.13 5.11
3 2.15 Ideal G SI2 62.6 54 18791 8.29 8.35 5.21
4 2.05 Ideal G SI1 61.9 57 18787 8.10 8.16 5.03
5 1.60 Ideal F VS1 62.0 56 18780 7.47 7.52 4.65
6 2.06 Ideal I VS2 62.2 55 18779 8.15 8.19 5.08
7 1.71 Ideal G VVS2 62.1 55 18768 7.66 7.63 4.75
> head(xy[order(xy$price, decreasing = TRUE), ], 7)
# A tibble: 7 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 1.51 Ideal G IF 61.7 55 18806 7.37 7.41 4.56
2 2.07 Ideal G SI2 62.5 55 18804 8.20 8.13 5.11
3 2.15 Ideal G SI2 62.6 54 18791 8.29 8.35 5.21
4 2.05 Ideal G SI1 61.9 57 18787 8.10 8.16 5.03
5 1.60 Ideal F VS1 62.0 56 18780 7.47 7.52 4.65
6 2.06 Ideal I VS2 62.2 55 18779 8.15 8.19 5.08
7 1.71 Ideal G VVS2 62.1 55 18768 7.66 7.63 4.75