You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言按分组统计dataframe各列含"<"非检出值的实现方法

批量统计多列非检出值数量实现需求

我有一个名为 data_total 的数据帧,结构预览如下:

Cuenca    Temporada Alcalinidad Carb_ Bicarb_ Fluoruros  Cloruros Nitratos Nitratos_N Sulfatos Nitritos Nitritos_N Fosfatos
  <fct>     <fct>           <dbl> <dbl>   <dbl> <chr>         <dbl> <chr>    <chr>         <dbl> <chr>    <chr>      <chr>   
1 Tumbes    Avenida          283.   0.3    346. 0.495         572.  <0.031   <0.007        901.  <0.003   <0.001     <0.019  
2 Zarumilla Avenida          146.   0.3    179. 0.2439999…    279.  <0.031   <0.007        274.  <0.003   <0.001     <0.019  
3 Zarumilla Avenida          108.   0.3    132. 0.314          62.8 2.22900… 0.504          61.0 <0.003   <0.001     <0.019  
4 Zarumilla Avenida          138.   0.3    168. 0.184         584.  <0.031   <0.007        151.  <0.003   <0.001     <0.019  
5 Tumbes    Avenida          156.   0.3    191. 0.2020000…     68.6 0.151    3.4000000…    124.  <0.003   <0.001     <0.019  
6 Tumbes    Avenida          211    0.3    257. 0.157          64.9 1.04499… 0.2359999…    146.  <0.003   <0.001     <0.019

核心需求

  • 按 Cuenca、Temporada 字段分组(后续支持灵活扩展更多分组维度)
  • 统计范围为数据帧中从Alcalinidad列到最后一列的所有字段
  • 统计指标:每个分组下,各字段内含 "<" 符号的非检出值数量、对应正常检出值数量

现有单列表统计实现

目前已完成单列(以Cd_dis字段为例)的统计逻辑,代码如下:

data_total %>% filter(str_detect(Cd_dis, "<")) %>% 
    group_by(Cuenca,Temporada) %>%
    summarise(non_detect = n(), completed= nrow(conteo)-n())

代码运行输出结果如下:

Cuenca            Temporada non_detect completed
  <fct>             <fct>          <int>     <int>
1 Intercuenca 13951 Avenida            4        95
2 Tumbes            Avenida           23        76
3 Zarumilla         Avenida           13        86

待实现功能

需要通用实现代码,支持批量遍历所有目标列、自动合并各变量的统计结果,无需逐列手动编写统计逻辑。

补充说明:我正在开发面向地质领域的R包,用于自动处理含非检出值的数据集,后续将采用美国地质调查局Hesel方法完成数据插补,所有提供有效帮助的贡献者都将在包的致谢部分署名。

内容的提问来源于stack exchange,提问作者Alonso Otiniano Zavala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 10:30:45