【问题标题】:in R programming, how to find the 7 most expensive prices for diamonds(dataset) of Ideal cut?在 R 编程中,如何找到理想切割的钻石(数据集)的 7 个最昂贵的价格?
【发布时间】:2018-04-03 03:21:29
【问题描述】:

我在一个名为 Diamonds 的数据集上做 R 编程问题。首先是为了安装和加载数据集。输入以下命令

install.packages("ggplot2")
library(ggplot2)
diamonds

现在因为有这么多的值和名称,我需要找出理想切工钻石(数据集)的 7 个最昂贵的价格?

我所做的是,我创建了一个名为 diamond.ideal 的数据框,并在数据框内放置了来自数据集 diamonds 的 3 列和值。这是代码

diamond.ideal <- data.frame(diamonds$cut,diamonds$color, diamonds$price)
head(diamond.ideal) #or diamond.ideal

这是输出的截图

最后,我需要找出最贵的 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 个最昂贵的价格?

【问题讨论】:

    标签: r dataframe max analysis


    【解决方案1】:

    有几种方法可以做到这一点。这是一个采用您的方法。

    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
    

    【讨论】:

      猜你喜欢
      • 2019-07-15
      • 2021-03-05
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      相关资源
      最近更新 更多