在 R 编程中,如何找到理想切割钻石(数据集)的 7 个最昂贵的价格?
in R programming, how to find the 7 most expensive prices for diamonds(dataset) of Ideal cut?
我正在做关于名为 Diamonds 的数据集的 R 编程问题。首先是为了安装和加载数据集。输入以下命令
install.packages("ggplot2")
library(ggplot2)
diamonds
现在因为有这么多的值和名称,我需要找出理想切割钻石(数据集)的 7 个最昂贵的价格?
我所做的是,我创建了一个名为 diamond.ideal 的数据框,并在该数据框内放置了 3 列和来自数据集菱形的值。这是代码
diamond.ideal <- data.frame(diamonds$cut,diamonds$color, diamonds$price)
head(diamond.ideal) #or diamond.ideal
这是输出的屏幕截图
最后,我需要找到 Ideal cut 钻石最贵的 7 个价格?
这是我的代码但不确定它是否正确
diamond.ideal[which(diamond.ideal$diamonds.cut == "Ideal", diamond.ideal$diamonds.price == max(diamond.cut$diamonds.price))[990:997],]
[990:997] 是行号(我认为)
这是屏幕截图
我不知道数字是否正确,数据集上的值太多了。
我只想知道最高值是否正确?
或者是否有另一种方法可以通过使用 table() 或 cut() 或其他函数来找到 Ideal 钻石的 7 个最昂贵的价格?
有几种方法可以做到这一点。这是一个采用您的方法的方法。
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
我正在做关于名为 Diamonds 的数据集的 R 编程问题。首先是为了安装和加载数据集。输入以下命令
install.packages("ggplot2")
library(ggplot2)
diamonds
现在因为有这么多的值和名称,我需要找出理想切割钻石(数据集)的 7 个最昂贵的价格?
我所做的是,我创建了一个名为 diamond.ideal 的数据框,并在该数据框内放置了 3 列和来自数据集菱形的值。这是代码
diamond.ideal <- data.frame(diamonds$cut,diamonds$color, diamonds$price)
head(diamond.ideal) #or diamond.ideal
这是输出的屏幕截图
最后,我需要找到 Ideal cut 钻石最贵的 7 个价格? 这是我的代码但不确定它是否正确
diamond.ideal[which(diamond.ideal$diamonds.cut == "Ideal", diamond.ideal$diamonds.price == max(diamond.cut$diamonds.price))[990:997],]
[990:997] 是行号(我认为) 这是屏幕截图
我不知道数字是否正确,数据集上的值太多了。 我只想知道最高值是否正确? 或者是否有另一种方法可以通过使用 table() 或 cut() 或其他函数来找到 Ideal 钻石的 7 个最昂贵的价格?
有几种方法可以做到这一点。这是一个采用您的方法的方法。
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