首页 > 解决方案 > 条件过滤后的统计

问题描述

如果已经在其他地方提出过这个问题,请提前道歉。

我有多个数据框(有 25 列和 > 1000 行),如下所示:

> head(Amsterdam_C02 <- Amsterdam %>% filter(Chemicals == "CO2"))
  Sample_ID Locality.Name       Chemicals  
1 VKB19xxxxxx     Amsterdam        CO2     
2 VKB19xxxxxx     Amsterdam        CO2     
3 VKB1xxxxxxx     Amsterdam        CO2     
4 VKB1xxxxxxx     Amsterdam        CO2     
5 VKB1xxxxxxx     Amsterdam        CO2     
6 VKB1xxxxxxx     Amsterdam        CO2      
End.Date  Less.Than Activity.Value  Measuring.Unit 
1 2019-01-31  <      1.0714000     g/m³                                                      
2 2019-02-18         3.4609000     g/m³                                                     
3 2019-02-28  <      0.7020623     g/m³                                                      
4 2019-04-25         4.5563282     g/m³                                                      
5 2019-05-20         1.6000000     g/m³                                                       
6 2019-05-22  <      0.6000000     g/m³     

我希望获得Activity.Value的平均值、最大值、最小值和sd,按月份分类,并且仅当Less.Than不是“<”时才考虑这些值(“<”表示该值低于检测限并且不会保留用于统计)。此示例每月显示一个或两个值,但有数百个。因此,R 必须每月返回 Activity.value 的 4 个统计信息(以向量或其他形式)。如果某个特定月份没有任何值高于检测限,则 R 必须为 4 个统计信息返回“-”。

此外,我希望 R 返回未考虑在内的全年所​​有值的平均值(每月未分类),因为 Less.Than 是“<”。

我做了不同的尝试,但都没有正常工作,我更愿意寻求你的帮助。

If Less.Than == "<" ???
为了每月过滤,我已经尝试了%>% filter(grepl("2019-01")12 次,但如果可能的话,我更愿意避免手动进行,因为我还有其他具有类似分析的数据框可以对其执行。

标签: rfilterdplyr

解决方案


不幸的是,您拥有的数据数量非常有限。我已经从上面获取了第二个数据——因为你的问题不需要考虑前半部分。

Less.Than列更改为,mutate以便在缺少 < 的地方引入所有 NA。由于您的数据框非常小,我添加了一个新的数据行。结果sd显示 NA 因为没有足够的数据,您可以通过n摘要中的数据数量看到。

然后过滤所有在End.Date中具有 NA 的行,按月分组并使用summarisefrom dplyr


新编辑:df 是一个 data.frame,如您所见class(df)。你也可以看看这里。然后我做了两个选择。一个是过滤Less.Than中的所有 NA 。这些是没有“<”的那些。而第二个则相反。全部按月分组。请记住,我已经在数据中添加了一行以至少获得一次 sd。

library(tidyverse)

df <- tribble(
  ~End.Date,  ~Less.Than, ~Activity.Value, 
   '2019-01-31',  '<' ,     1.0714000,                                                      
   '2019-02-18',   '' ,     3.4609000,                                                     
   '2019-02-28',  '<' ,     0.7020623,                                                      
   '2019-04-25',  '' ,     4.5563282,                                                      
   '2019-05-20',  '' ,     1.6000000,                                                       
   '2019-05-22',  '<' ,     0.6000000,
   '2019-05-22',  '<' ,     0.7000000
)
df$End.Date <- as.Date(df$End.Date)

df
#> # A tibble: 7 x 3
#>   End.Date   Less.Than Activity.Value
#>   <date>     <chr>              <dbl>
#> 1 2019-01-31 "<"                1.07 
#> 2 2019-02-18 ""                 3.46 
#> 3 2019-02-28 "<"                0.702
#> 4 2019-04-25 ""                 4.56 
#> 5 2019-05-20 ""                 1.6  
#> 6 2019-05-22 "<"                0.6  
#> 7 2019-05-22 "<"                0.7
# here you can see that the df is a data.frame
class(df)
#> [1] "tbl_df"     "tbl"        "data.frame"

df %>% 
  mutate(Less.Than = ifelse(Less.Than != '<', NA, Less.Than)) %>% 
  # what follows filters the rows which contain NA
  dplyr::filter(is.na(Less.Than)) %>% 
  group_by(months(End.Date)) %>% 
  summarise(
    sum = sum(Activity.Value),
    min = min(Activity.Value),
    sd = sd(Activity.Value),
    n = n())
#> # A tibble: 3 x 5
#>   `months(End.Date)`   sum   min    sd     n
#>   <chr>              <dbl> <dbl> <dbl> <int>
#> 1 April               4.56  4.56    NA     1
#> 2 Februar             3.46  3.46    NA     1
#> 3 Mai                 1.6   1.6     NA     1

df %>% 
  mutate(Less.Than = ifelse(Less.Than != '<', NA, Less.Than)) %>% 
  # what follows filters the rows which DO NOT contain NA
  # or in your words these rows possess a "<"
  dplyr::filter(!is.na(Less.Than)) %>% 
  group_by(months(End.Date)) %>% 
  summarise(
    sum = sum(Activity.Value),
    min = min(Activity.Value),
    sd = sd(Activity.Value),
    n = n())
#> # A tibble: 3 x 5
#>   `months(End.Date)`   sum   min      sd     n
#>   <chr>              <dbl> <dbl>   <dbl> <int>
#> 1 Februar            0.702 0.702 NA          1
#> 2 Januar             1.07  1.07  NA          1
#> 3 Mai                1.30  0.6    0.0707     2

reprex 包(v0.3.0)于 2020 年 6 月 15 日创建


推荐阅读