gpt4 book ai didi

r - R 中带有子集的 For 循环

转载 作者:行者123 更新时间:2023-12-01 14:00:54 25 4
gpt4 key购买 nike

我在 csv 文件中有以下数据:

Date        Model      Color    Value   Samples
6/19/2017 Gold Blue 0.5 500
6/19/2017 Gold Red 0.0 449
6/19/2017 Silver Blue 0.75 1320
6/19/2017 Silver Blue 1.5 103
6/19/2017 Gold Red 0.7 891
6/19/2017 Gold Blue 0.41 18103
6/19/2017 Copper Blue 0.83 564
6/19/2017 Silver Pink 1.17 173
6/19/2017 Platinum Brown 0.43 793
6/19/2017 Platinum Red 0.71 1763
6/19/2017 Gold Orange 1.92 503

我使用 fread 函数来创建一个 data.table:
library(dplyr)
library(data.table)

df <- fread("test_data.csv",
header = TRUE,
fill = TRUE,
sep = ",")

然后我通过 Model 对数据进行子集化,如下所示:
df_subset <- subset(df, df$Model=='Gold' & df$Value > 0)

然后,我根据 Color 变量创建了一些百分位数,如下所示:
df_subset[, .(Samples = sum(Samples),
'50th' = quantile(AvgValue, probs = c(0.50)),
'99th' = quantile(AvgValue, probs = c(0.99)),
'99.9th' = quantile(AvgValue, probs = c(0.999)),
'99.99th' = quantile(AvgValue, probs = c(0.9999))),
by = Color]

这给出了以下输出:
    Color Samples  50th   99th  99.9th  99.99th
1: Blue 18603 0.455 0.4991 0.49991 0.499991
2: Red 1340 0.975 1.2445 1.24945 1.249945
3: Orange 503 1.920 1.9200 1.92000 1.920000

我正在尝试遍历 Model 值列表并输出每个 Model 值的相关百分位值。

我已经尝试了以下(失败):
models <- unique(df$Model)

for (model in models){

df$model[, .(Samples = sum(Samples),
'50th' = quantile(Value, probs = c(0.50)),
'99th' = quantile(Value, probs = c(0.99)),
'99.9th' = quantile(Value, probs = c(0.999)),
'99.99th' = quantile(Value, probs = c(0.9999))),
by = Color]
}

错误信息是:
Error in .(Samples = sum(Samples), `50th` = quantile(Value, probs = c(0.5)),  :  could not find function "."

最佳答案

fread 创建一个 data.table 对象而不是一个数据框,所以我建议坚持使用 data.table 语法而不是将它与 dplyr 混淆。也不需要 for 循环,我们可以使用 by 参数中的两个变量列表在一行代码中循环模型和颜色:

qs = df[Value > 0, .(Samples = sum(Samples),
'50th' = quantile(Value, probs = c(0.50)),
'99th' = quantile(Value, probs = c(0.99)),
'99.9th' = quantile(Value, probs = c(0.999)),
'99.99th' = quantile(Value, probs = c(0.9999))),
by = .(Model, Color)]
setkey(qs, 'Model')

# Model Color Samples 50th 99th 99.9th 99.99th
# 1: Copper Blue 564 0.830 0.8300 0.83000 0.830000
# 2: Gold Blue 18603 0.455 0.4991 0.49991 0.499991
# 3: Gold Red 891 0.700 0.7000 0.70000 0.700000
# 4: Gold Orange 503 1.920 1.9200 1.92000 1.920000
# 5: Platinum Brown 793 0.430 0.4300 0.43000 0.430000
# 6: Platinum Red 1763 0.710 0.7100 0.71000 0.710000
# 7: Silver Blue 1423 1.125 1.4925 1.49925 1.499925
# 8: Silver Pink 173 1.170 1.1700 1.17000 1.170000

关于r - R 中带有子集的 For 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45286990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com