Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中表格形式的百分位数据_R - Fatal编程技术网

R中表格形式的百分位数据

R中表格形式的百分位数据,r,R,我有一个数据帧(df),如下所示: Date Model Color Value Samples 6/19/17 Gold Blue 0.50 500 6/19/17 Gold Red 1.25 449 6/19/17 Silver Blue 0.75 1320 6/19/17 Silver Blue 1.50 103 6/19/17 Gold Red 0.70

我有一个数据帧(df),如下所示:

Date        Model   Color   Value  Samples
6/19/17     Gold    Blue    0.50   500
6/19/17     Gold    Red     1.25   449
6/19/17     Silver  Blue    0.75   1320
6/19/17     Silver  Blue    1.50   103
6/19/17     Gold    Red     0.70   891
6/19/17     Gold    Blue    0.41   18103
6/19/17     Copper  Blue    0.83   564
            |  # Samples  |  50th percentile |  99th percentile |  99.9th percentile |  99.99th percentile
Gold
Silver
Copper
我可以使用以下方法输出每个
颜色
变量的百分比数据:

df_subset <- subset(df, df$Color == 'Blue')
quantile(df_subset$Value, c(0.50, 0.99, 0.999, 0.9999))

提前感谢您的帮助

使用
dplyr
的解决方案
dt2
是最终输出

library(data.table)

dat <- data.table(Date = "6/19/17",
                  Model = c("Gold", "Gold", "Silver", "Silver", "Gold", "Gold", "Copper"),
                  Color = c("Blue", "Red", "Blue", "Blue", "Red", "Blue", "Blue"),
                  Value = c(0.5, 1.25, .75, 1.5, .7, .41, .83),
                  Samples = c(500, 449, 1320, 103, 891, 18103, 564))

dat[, .(Samples = sum(Samples),
        `50th percentile` = quantile(Value, probs = c(0.5)),
        `99th percentile` = quantile(Value, probs = c(0.99)),
        `99.9th percentile` = quantile(Value, probs = c(0.999)),
        `99.99th percentile` = quantile(Value, probs = c(0.9999))), 
    by = Model]
dt <- read.table(text = "Date        Model   Color   Value  Samples
6/19/17     Gold    Blue    0.50   500
                 6/19/17     Gold    Red     1.25   449
                 6/19/17     Silver  Blue    0.75   1320
                 6/19/17     Silver  Blue    1.50   103
                 6/19/17     Gold    Red     0.70   891
                 6/19/17     Gold    Blue    0.41   18103
                 6/19/17     Copper  Blue    0.83   564",
                 header = TRUE, stringsAsFactors = FALSE)

library(dplyr)

dt2 <- dt %>%
  group_by(Model) %>%
  summarise(Samples = sum(Samples),
            `50th percentile` = quantile(Value, 0.5),
            `99th percentile` = quantile(Value, 0.99),
            `99.9th percentile` = quantile(Value, 0.999),
            `99.99th percentile` = quantile(Value, 0.9999))
dt%
总结(样本=总和(样本),
`第50百分位`=分位数(值,0.5),
`第99百分位`=分位数(值,0.99),
`99.9%分位数`=分位数(值,0.999),
`第99.99百分位`=分位数(值,0.9999))
dt <- read.table(text = "Date        Model   Color   Value  Samples
6/19/17     Gold    Blue    0.50   500
                 6/19/17     Gold    Red     1.25   449
                 6/19/17     Silver  Blue    0.75   1320
                 6/19/17     Silver  Blue    1.50   103
                 6/19/17     Gold    Red     0.70   891
                 6/19/17     Gold    Blue    0.41   18103
                 6/19/17     Copper  Blue    0.83   564",
                 header = TRUE, stringsAsFactors = FALSE)

library(dplyr)

dt2 <- dt %>%
  group_by(Model) %>%
  summarise(Samples = sum(Samples),
            `50th percentile` = quantile(Value, 0.5),
            `99th percentile` = quantile(Value, 0.99),
            `99.9th percentile` = quantile(Value, 0.999),
            `99.99th percentile` = quantile(Value, 0.9999))