我试图基于环境的选择列表进行过滤,以便用户选择要比较的两种环境
glimpse(df)
Observations: 739
Variables: 6
$ svcname <chr> "Forecasts", "15minLoadForeca...
$ application <chr> "app1", "app2", "app3", "app1", "app2", "app3", "app1"...
$ environment <chr> "MAPStage", "MAPTest", "Production", "Training", "MAPS...
$ payload_size <dbl> 152.24, 104.64, 153.28, 149.25, 309.80, 3.12, 653.55, ...
$ dt <date> 2018-09-11, 2018-09-11, 2018-09-11, 2018-09-11, 2018-...
$ id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,...
我从环境
创建了一个唯一列表 (lst_envc <- unique(df$environment))
lst_envc
[1]“ MPStage”“ MPTest”“生产”“培训”“ Stage”
[6]“测试”
我根据 environment 和 payload_size 传播了这个df,结果是df_new并用0填充空单元格
df_new <- tidyr::spread(df,environment,payload_size,fill = 0
Observations: 739
Variables: 10
$ svcname <chr> "Forecasts", "15LoadForecas...
$ application <chr> "app1", "app2", "app3", "app1", "app2", "app3", "app1",...
$ dt <date> 2018-09-11, 2018-09-11, 2018-09-11, 2018-09-11, 2018-0...
$ id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, ...
$ MPStage <dbl> 152.24, 0.00, 0.00, 0.00, 309.80, 0.00, 0.00, 0.00, 164...
$ MPTest <dbl> 0.00, 104.64, 0.00, 0.00, 0.00, 3.12, 0.00, 0.00, 0.00,...
$ Production <dbl> 0.00, 0.00, 153.28, 0.00, 0.00, 0.00, 653.55, 0.00, 0.0...
$ Stage <dbl> 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0...
$ Test <dbl> 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0...
$ Training <dbl> 0.00, 0.00, 0.00, 149.25, 0.00, 0.00, 0.00, 393.07, 0.0...
此视图将删除lst_envc [[1]和 Prod
代表的 MPStage 以外的所有环境。我尝试创建另一个仅具有一个环境的df,并仅过滤出大于0的值这是过滤失败的地方
df_subset <- df_new %>%
dplyr::select(., svcname, application, dt, id, lst_envc[[1]], Production)
A tibble: 739 x 6
Groups: svcname, application [189]
svcname application dt id MAPStage Production
<chr> <chr> <date> <int> <dbl> <dbl>
1 15NForecasts app 2018-09-11 1 152.24 0.00
2 15NForecasts app 2018-09-11 2 0.00 0.00
3 15NForecasts app 2018-09-11 3 0.00 153.28
4 15NForecasts app 2018-09-11 4 0.00 0.00
5 5LForecast app 2018-09-11 5 309.80 0.00
6 5LForecast app 2018-09-11 6 0.00 0.00
7 5LForecast app 2018-09-11 7 0.00 653.55
8 5LForecast app 2018-09-11 8 0.00 0.00
9 5NForecasts app 2018-09-11 9 164.62 0.00
10 5NForecasts app 2018-09-11 10 0.00 0.00
... with 729 more rows
another_df <- df_subset%>%
select(., - Production) %>%
filter(lst_envc[[1]] > 0)
我应该只有128行,但是没有任何内容被过滤掉:(
A tibble: 739 x 5
Groups: svcname, application [189]
svcname application dt id MPStage
<chr> <chr> <date> <int> <dbl>
1 Forecasts app 2018-09-11 1 152.24
2 Forecasts app 2018-09-11 2 0.00
3 Forecasts app 2018-09-11 3 0.00
4 Forecasts app 2018-09-11 4 0.00
5 Forecast app 2018-09-11 5 309.80
6 Forecast app 2018-09-11 6 0.00
7 Forecast app 2018-09-11 7 0.00
8 Forecast app 2018-09-11 8 0.00
9 5LoadForecasts app 2018-09-11 9 164.62
10 5LoadForecasts app 2018-09-11 10 0.00
... with 729 more row
答案 0 :(得分:0)
也许是dplyr的filter_at吗?
another_df <- df_subset %>% select(., - Production) %>% filter_at(lst_envc, all_vars(. > 0))