从 table 中提取有用的信息来预测真实场景
Extract useful information from a table to predict real scenarios
假设我是一家快递公司的主管,我想说明我的产品在哪些条件下能以良好的质量送达收件人(例如,没有破损、仍然完好等)。
Product operator1 operator2 operator3 operator4 weather day quality
product1 x 0 0 x sun mon x
product2 0 x 0 0 grey tue 0
product3 x x x x grey mon x
product4 0 x 0 0 cloudy fri 0
product5 0 0 x x grey sat 0
product6 x x 0 0 grey wed x
product7 x 0 0 x cloudy wed 0
product8 0 0 x x grey wed x
product9 x 0 0 0 grey mon 0
product10 0 0 x 0 cloudy tue x
所以我有这个(代理)table,我想以任意组合和数量的变量提取此类信息,例如:
Operator1 = x
Operator2 = x
Operator3 = x
Operator4 = x
Weather = sunny
Day = mon
Operator1 = x + Operator2 = x
Operator1 = x + weather = sunny
etc..
测试(优质产品的数量)/(根据特定变量值过滤的产品的数量)的比率是否满足假设 > 0.8。通过这种方式,我可以确定什么是交付我的产品的最佳条件。
我知道这是一个复杂的问题,但我希望有人知道某种方式或某些特定的(最好是 R)包可以解决这个问题。
当然可以做你想做的事,我会在这里给你一个例子来说明如何实现,但看起来你可以通过阅读一本很好的 R 入门来理解不同的数据类型和数据操作的基本概念。
首先,我会将您的数据副本作为数据框加载到 R 中:
data.frame(Product = paste0("product", 1:10),
operator1 = unlist(strsplit("x0x00xx0x0", "")),
operator2 = unlist(strsplit("0xxx0x0000", "")),
operator3 = unlist(strsplit("00x0x00x0x", "")),
operator4 = unlist(strsplit("x0x0x0xx00", "")),
weather = c("sun", "grey", "grey", "cloudy", "grey",
"grey", "cloudy", "grey", "grey", "grey"),
day = c("mon", "tue", "mon", "fri", "sat",
"wed", "wed", "wed", "mon", "tue"),
quality = unlist(strsplit("x0x00x0x0x", ""))) -> deliveries
要显示这与您的数据相同:
> deliveries
Product operator1 operator2 operator3 operator4 weather day quality
1 product1 x 0 0 x sun mon x
2 product2 0 x 0 0 grey tue 0
3 product3 x x x x grey mon x
4 product4 0 x 0 0 cloudy fri 0
5 product5 0 0 x x grey sat 0
6 product6 x x 0 0 grey wed x
7 product7 x 0 0 x cloudy wed 0
8 product8 0 0 x x grey wed x
9 product9 x 0 0 0 grey mon 0
10 product10 0 0 x 0 grey tue x
现在,我们需要将您的table转换成可以正常处理的格式。 0 和 x 的列不会像您预期的那样被 R 理解,因此您需要将它们转换为 TRUE/FALSE 值或 0/1 值。我们可以在 dplyr
包的帮助下做到这一点,它是 tidyverse
:
的一部分
install.packages("tidyverse")
library(tidyverse)
deliveries %<>% transmute(Product = Product,
operator1 = as.numeric(operator1 == "x"),
operator2 = as.numeric(operator2 == "x"),
operator3 = as.numeric(operator3 == "x"),
operator4 = as.numeric(operator4 == "x"),
weather = weather,
day = day,
quality = as.numeric(quality == "x"))
现在您的数据如下所示:
Product operator1 operator2 operator3 operator4 weather day quality
1 product1 1 0 0 1 sun mon 1
2 product2 0 1 0 0 grey tue 0
3 product3 1 1 1 1 grey mon 1
4 product4 0 1 0 0 cloudy fri 0
5 product5 0 0 1 1 grey sat 0
6 product6 1 1 0 0 grey wed 1
7 product7 1 0 0 1 cloudy wed 0
8 product8 0 0 1 1 grey wed 1
9 product9 1 0 0 0 grey mon 0
10 product10 0 0 1 0 grey tue 1
现在您可以通过将列名称放入 group_by 函数来汇总您选择的任何组合的比例。在这种情况下,让我们做 operator1、day 和 weather:
deliveries %>%
group_by(operator1, day, weather) %>%
summarise(quality = paste0(mean(quality) * 100, "%"))
这有望给出您正在寻找的那种结果,并显示每个变量组合的质量交付比例。
# A tibble: 8 x 4
# Groups: operator1, day [?]
operator1 day weather quality
<dbl> <fct> <fct> <chr>
1 0 fri cloudy 0%
2 0 sat grey 0%
3 0 tue grey 50%
4 0 wed grey 100%
5 1 mon grey 50%
6 1 mon sun 100%
7 1 wed cloudy 0%
8 1 wed grey 100%
如果您想查看不同的组合,请将传递给 group_by
的变量更改为您想要的任何值。
假设我是一家快递公司的主管,我想说明我的产品在哪些条件下能以良好的质量送达收件人(例如,没有破损、仍然完好等)。
Product operator1 operator2 operator3 operator4 weather day quality
product1 x 0 0 x sun mon x
product2 0 x 0 0 grey tue 0
product3 x x x x grey mon x
product4 0 x 0 0 cloudy fri 0
product5 0 0 x x grey sat 0
product6 x x 0 0 grey wed x
product7 x 0 0 x cloudy wed 0
product8 0 0 x x grey wed x
product9 x 0 0 0 grey mon 0
product10 0 0 x 0 cloudy tue x
所以我有这个(代理)table,我想以任意组合和数量的变量提取此类信息,例如:
Operator1 = x
Operator2 = x
Operator3 = x
Operator4 = x
Weather = sunny
Day = mon
Operator1 = x + Operator2 = x
Operator1 = x + weather = sunny
etc..
测试(优质产品的数量)/(根据特定变量值过滤的产品的数量)的比率是否满足假设 > 0.8。通过这种方式,我可以确定什么是交付我的产品的最佳条件。 我知道这是一个复杂的问题,但我希望有人知道某种方式或某些特定的(最好是 R)包可以解决这个问题。
当然可以做你想做的事,我会在这里给你一个例子来说明如何实现,但看起来你可以通过阅读一本很好的 R 入门来理解不同的数据类型和数据操作的基本概念。
首先,我会将您的数据副本作为数据框加载到 R 中:
data.frame(Product = paste0("product", 1:10),
operator1 = unlist(strsplit("x0x00xx0x0", "")),
operator2 = unlist(strsplit("0xxx0x0000", "")),
operator3 = unlist(strsplit("00x0x00x0x", "")),
operator4 = unlist(strsplit("x0x0x0xx00", "")),
weather = c("sun", "grey", "grey", "cloudy", "grey",
"grey", "cloudy", "grey", "grey", "grey"),
day = c("mon", "tue", "mon", "fri", "sat",
"wed", "wed", "wed", "mon", "tue"),
quality = unlist(strsplit("x0x00x0x0x", ""))) -> deliveries
要显示这与您的数据相同:
> deliveries
Product operator1 operator2 operator3 operator4 weather day quality
1 product1 x 0 0 x sun mon x
2 product2 0 x 0 0 grey tue 0
3 product3 x x x x grey mon x
4 product4 0 x 0 0 cloudy fri 0
5 product5 0 0 x x grey sat 0
6 product6 x x 0 0 grey wed x
7 product7 x 0 0 x cloudy wed 0
8 product8 0 0 x x grey wed x
9 product9 x 0 0 0 grey mon 0
10 product10 0 0 x 0 grey tue x
现在,我们需要将您的table转换成可以正常处理的格式。 0 和 x 的列不会像您预期的那样被 R 理解,因此您需要将它们转换为 TRUE/FALSE 值或 0/1 值。我们可以在 dplyr
包的帮助下做到这一点,它是 tidyverse
:
install.packages("tidyverse")
library(tidyverse)
deliveries %<>% transmute(Product = Product,
operator1 = as.numeric(operator1 == "x"),
operator2 = as.numeric(operator2 == "x"),
operator3 = as.numeric(operator3 == "x"),
operator4 = as.numeric(operator4 == "x"),
weather = weather,
day = day,
quality = as.numeric(quality == "x"))
现在您的数据如下所示:
Product operator1 operator2 operator3 operator4 weather day quality
1 product1 1 0 0 1 sun mon 1
2 product2 0 1 0 0 grey tue 0
3 product3 1 1 1 1 grey mon 1
4 product4 0 1 0 0 cloudy fri 0
5 product5 0 0 1 1 grey sat 0
6 product6 1 1 0 0 grey wed 1
7 product7 1 0 0 1 cloudy wed 0
8 product8 0 0 1 1 grey wed 1
9 product9 1 0 0 0 grey mon 0
10 product10 0 0 1 0 grey tue 1
现在您可以通过将列名称放入 group_by 函数来汇总您选择的任何组合的比例。在这种情况下,让我们做 operator1、day 和 weather:
deliveries %>%
group_by(operator1, day, weather) %>%
summarise(quality = paste0(mean(quality) * 100, "%"))
这有望给出您正在寻找的那种结果,并显示每个变量组合的质量交付比例。
# A tibble: 8 x 4
# Groups: operator1, day [?]
operator1 day weather quality
<dbl> <fct> <fct> <chr>
1 0 fri cloudy 0%
2 0 sat grey 0%
3 0 tue grey 50%
4 0 wed grey 100%
5 1 mon grey 50%
6 1 mon sun 100%
7 1 wed cloudy 0%
8 1 wed grey 100%
如果您想查看不同的组合,请将传递给 group_by
的变量更改为您想要的任何值。