无法根据dbl值过滤列表中的行

问题描述 投票:1回答:1

我试图根据环境中的选择列表进行过滤,以便用户选择要比较的两个环境

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”“生产”“训练”“舞台” [6]“测试”

我根据环境和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 
r dplyr tidyr
1个回答
0
投票

也许来自dplyr的filter_at?

another_df <- df_subset %>% select(., - Production) %>%  filter_at(lst_envc, all_vars(. > 0))
© www.soinside.com 2019 - 2024. All rights reserved.