条件过滤后的统计信息

时间:2020-06-15 11:12:15

标签: r filter dplyr

如果已经在其他地方提出要求,请提前道歉。

我有多个这样的数据帧(具有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的均值,最大值,最小值和标准差,按月分类,并且仅在Less.Than不是“ <” (“ <”表示该值低于检测极限,将不会保留以进行统计。 本示例每月显示一个或两个值,但是有数百个。因此,R必须每月(以向量或其他方式)返回Activity.value的4个统计信息。如果一个月没有超出检测极限的值,则R必须为这4个统计信息返回“-”。

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

我做了不同的尝试,但没有正常工作,我希望寻求您的帮助。

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

2 个答案:

答案 0 :(得分:1)

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

列{em> Less.Than 用mutate进行了更改,因此在<丢失的地方引入了所有NA。由于您的数据框很小,因此我添加了一个新的数据行。 sd的结果显示为NA,因为没有足够的数据,如摘要中的数据n所示。

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


新编辑: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-06-15创建

答案 1 :(得分:0)

您可以使用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

相关问题