如何基于标准将新观察值添加到数据框?

时间:2019-06-17 20:20:13

标签: r tidyverse

我有一个在多个时间段内多个站点的结果表(test.df)。土壤参数之一分为两个参数(DDT 2和DDT 4)。我想将它们加在一起,然后将它们重新添加到数据框中。

我可以像这样总结这些结果:

test1 <- test %>% 
  select(Sample,Site,Parameter,Value) %>% 
  filter(str_detect(Parameter,"DDT")) 

test1 %>% 
  group_by(Sample,Site) %>% 
  count(sum(Value))

但是我不确定如何将它们重新添加到原始数据框中

我的原始数据框(test.df)看起来像这样(但是有更多的站点和示例)

 Sample         Site  Parameter       Value
   2014-01-27   3999  Soil 2,4'-DDT   0.010
   2014-01-27   3999  Soil 4,4-DDT    0.020
   2014-01-27   3999  Soil 4,4 FB     0.020
   2013-01-31   4040  Soil 2,4'-DDT   0.010
   2013-01-31   4040  Soil 4,4-DDT    0.010
   2014-01-27   4040  Soil 4,4 EC     0.020

我想对每个站点的DDT值求和,然后重新添加到表中,如下所示:

 Sample         Site  Parameter      Value
   2014-01-27   3999  Soil 2,4'-DDT  0.010
   2014-01-27   3999  Soil 4,4-DDT   0.020
   2014-01-27   3999  Soil 4,4 EC    0.020
   2014-01-27   3999  Total DDT      0.030
   2013-01-31   4040  Soil 2,4'-DDT  0.010
   2013-01-31   4040  Soil 4,4-DDT   0.010
   2014-01-27   4040  Soil 4,4 EC    0.020
   2013-01-31   4040  Total DDT      0.020

1 个答案:

答案 0 :(得分:1)

我希望这种方法对您有用:

data<- data.frame(Sample= c("2014-01-27","2014-01-27","2013-01-01","2013-01-01"),
                  SiteID= c(3999,3999,4040,4040),      
                  DetDesc= c("Soil 2,4-DDT", "Soil 4,4-DDT","Soil 2,4-DDT", "Soil 4,4-DDT"),
                  Value= c(0.010, 0.02, 0.010, 0.01)
                  )
library(dplyr)
data %>% group_by(SiteID, Sample) %>% 
  summarise(Value = sum(Value)) %>%mutate(DetDesc = "Total DDT")%>%
  select(Sample, SiteID, DetDesc, Value ) %>% bind_rows(data)
# A tibble: 6 x 4
# Groups:   SiteID [2]
  Sample     SiteID DetDesc      Value
  <fct>       <dbl> <chr>        <dbl>
1 2014-01-27   3999 Total DDT     0.03
2 2013-01-01   4040 Total DDT     0.02
3 2014-01-27   3999 Soil 2,4-DDT  0.01
4 2014-01-27   3999 Soil 4,4-DDT  0.02
5 2013-01-01   4040 Soil 2,4-DDT  0.01
6 2013-01-01   4040 Soil 4,4-DDT  0.01

如果您也希望不按Sample列进行分组,只需删除它即可。 干杯!