【问题标题】:Statistics after conditional filtering条件过滤后的统计
【发布时间】:2020-10-04 19:01:07
【问题描述】:

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

我有多个数据框(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 返回未考虑在内的全年所​​有值的平均值(每月未分类),因为 Less.Than 是“

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

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

【问题讨论】:

    标签: r filter dplyr


    【解决方案1】:

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

    Less.Than 列更改为 mutate,以便在缺少 sd 的结果显示 NA,因为没有足够的数据,您可以从摘要中的数据数量 n 看到。

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


    新编辑:df 是一个 data.frame,您可以在 class(df) 看到。你也可以看看here。 然后我做了两个选择。一个是过滤 Less.Than 中的所有 NA。这些是没有“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 package (v0.3.0) 于 2020 年 6 月 15 日创建

    【讨论】:

    • 我不确定你的意思。你的评论有什么遗漏吗?
    • 抱歉数据有限。无论如何,谢谢你的提议。我有 3 条评论。对于上面给出的示例,您的脚本应该为 1 月的 4 个统计信息返回“-”,因为必须省略 Activity.Value,因为 Less.Than 是“
    • 我通过更改过滤器再次编辑了答案。一月现在没有显示,二月给出了你期望的结果-
    • 谢谢四位您的提议。我仍然无法计算在 Less.Than 中具有“
    【解决方案2】:

    你可以使用aggregate:

    可重现的数据:

    df <- data.frame(
      Date = c("2019-01-31", "2019-02-18", "2019-02-28", "2019-04-25", "2019-05-20", "2019-05-02"),
      Less.than = c("", "<", "", "<", "", ""),
      Activity.level = c(1.0714000, 3.4609000, 0.7020623, 0.7020623, 4.5563282, 1.6000000)) 
    

    求均值的解法:

    aggregate(df$Activity.level[!df$Less.than=="<"], by = list(sub("-(\\d+)-", "-\\1-", df$Date[!df$Less.than=="<"])), mean)
         Group.1         x
    1 2019-01-31 1.0714000
    2 2019-02-28 0.7020623
    3 2019-05-02 1.6000000
    4 2019-05-20 4.5563282
    

    对于其他统计信息,请相应地替换mean

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      相关资源
      最近更新 更多