简体   繁体   中英

Conditional statistics on R data frame

I have about 50 data frames for the analysis of air pollution. Here is an example:

> Amsterdam_CO2
   Chemicals Begin.Date   End.Date Less.Than    Value Uncertainty.Value Measuring.Unit
1    CO2 2019-01-31 2019-01-31         <      1.0714000                NA          Mol/KG
2    CO2 2019-02-28 2019-02-28         <      0.4609000                NA          Mol/KG
3    CO2 2019-03-28 2019-03-28         <      0.7020623                NA          Mol/KG
4    CO2 2019-04-25 2019-04-25         <      0.5563282                NA          Mol/KG
5    CO2 2019-05-22 2019-05-22         <      1.6000000                NA          Mol/KG
6    CO2 2019-06-20 2019-06-20         <      0.6000000                NA          Mol/KG
7    CO2 2019-07-09 2019-07-09         <      1.2000000                NA          Mol/KG
8    CO2 2019-08-12 2019-08-12         <      0.8000000                NA          Mol/KG
9    CO2 2019-09-11 2019-09-11         <      1.3000000                NA          Mol/KG
10   CO2 2019-10-10 2019-10-10         <      1.0000000                NA          Mol/KG
11   CO2 2019-11-04 2019-11-04                0.7000000                NA          Mol/KG
12   CO2 2019-12-05 2019-12-05                0.9000000                NA          Mol/KG

I want to create 2 new data frames representing the mean, max, min and stdv of 2 groups:

-the rows that contain "<" in Less.Than (indicating we are below the detection limit) called Amsterdam_CO2_BelowDL

-the rows that do not contain "<" in Less.Than (indicating we're above the delection limit) called Amsterdam_CO2_AboveDL .

#Filter and statistics for rows without "<" in Less.Than
Amsterdam_CO2_AboveDL <- Amsterdam_CO2 %>% 
              dplyr::filter(Less.Than != "<") %>% 
              (summarise(mean_Mesure = mean(Value), max_Mesure = max(Value), min_Mesure = min(Value), sd_Mesure = sd(Value), nbr_Mesure = n()))

> Amsterdam_CO2_AboveDL
    mean_Mesure max_Mesure min_Mesure     sd_Mesure nbr_Mesure
1       0.8         0.9        0.7           0.05      2

#Filter and statistics for rows with "<" in Less.Than         
Amsterdam_CO2_BelowDL <- Amsterdam_CO2 %>%
              dplyr::filter(Less.Than == "<") %>% 
              summarise(mean_DL = mean(Value), max_DL = max(Value), min_DL = min(Value), sd_DL = sd(Value), nbr_DL = n())

> Amsterdam_CO2_BelowDL
    mean_DL max_DL min_DL     sd_DL nbr_DL
1 0.9075575    1.6 0.4609 0.3396243     10

#export in an Excel file
wb = createWorkbook()
sheet1 = createSheet(wb, "Amsterdam_CO2")
cs3 <- CellStyle(wb) + Font(wb, isBold=TRUE) + Border()  # header

addDataFrame(Amsterdam_CO2, sheet=sheet1, startColumn=1, row.names=F)
addDataFrame(Amsterdam_CO2_AboveDL, sheet=sheet1, startRow=(3+nrow(Amsterdam_CO2)), row.names=F, showNA = F, characterNA = "", colnamesStyle=cs3)
addDataFrame(Amsterdam_CO2_BelowDL, sheet=sheet1, startRow=(5+nrow(Amsterdam_CO2)), row.names=F, showNA = F, characterNA = "", colnamesStyle=cs3)
            saveWorkbook(wb, "Amsterdam.xlsx")

However, for most of the initial data frames, all the values are below the delection limit, meaning all rows have "<". In this case, R fails to create one data frame (AboveDL) and returns an error for the deticated statistics:

Error in mean(Value) : object 'Value' not found

Therefore, I would like to add something ( if... else ?) explaining that if the data frame AboveDL or Below DL is empty (0x7 variables), then R must still return a data frame with:

mean = -, max = -, min = -, sd = -, nbr = 0

The goal is to obtain something quite automatic that will give 2 new exportable data frames, whatever the presence of "<" in the intial data frame.

#Filter and statistics for rows without "<" in Less.Than
Amsterdam_CO2_AboveDL <- Amsterdam_CO2 %>% 
              dplyr::filter(Less.Than != "<") %>% 
 ???? if (nrow(Amsterdam_CO2_AboveDL) > 0) 
{  (summarise(mean_Mesure = mean(Value), max_Mesure = max(Value), min_Mesure = min(Value), sd_Mesure = sd(Value), nbr_Mesure = n())) }

??? else {
mean = "-", max = "-", min = "-", sd = "-", nbr = "0" }


#Filter and statistics for rows with "<" in Less.Than         
Amsterdam_CO2_BelowDL <- Amsterdam_CO2 %>%
              dplyr::filter(Less.Than == "<") %>% 
 ???? if (nrow(Amsterdam_CO2_BelowDL) > 0) ???

              summarise(mean_DL = mean(Value), max_DL = max(Value), min_DL = min(Value), sd_DL = sd(Value), nbr_DL = n())
blank_df <- data.frame(mean = "-", max = "-", min = "-", sd = "-", nbr = "0")

Amsterdam_CO2_AboveDL <- dplyr::filter(Amsterdam_CO2, Less.Than != "<") %>% 
  dplyr::summarise(mean_Mesure = mean(Value),
                   max_Mesure = max(Value),
                   min_Mesure = min(Value),
                   sd_Mesure = sd(Value),
                   nbr_Mesure = n())

if (nrow(Amsterdam_CO2_AboveDL) == 0)
  Amsterdam_CO2_AboveDL <- blank_df

Amsterdam_CO2_BelowDL <- dplyr::filter(Amsterdam_CO2, Less.Than == "<") %>% 
  dplyr::summarise(mean_Mesure = mean(Value),
                   max_Mesure = max(Value),
                   min_Mesure = min(Value),
                   sd_Mesure = sd(Value),
                   nbr_Mesure = n())

if (nrow(Amsterdam_CO2_BelowDL) == 0)
  Amsterdam_CO2_BelowDL <- blank_df

wb = createWorkbook()
sheet1 = createSheet(wb, "Amsterdam_CO2")
cs3 <- CellStyle(wb) + Font(wb, isBold = TRUE) + Border()

addDataFrame(Amsterdam_CO2, sheet = sheet1, startColumn = 1, row.names = FALSE)
addDataFrame(Amsterdam_CO2_AboveDL,
             sheet = sheet1,
             startRow = (3+nrow(Amsterdam_CO2)),
             row.names = FALSE,
             showNA = FALSE,
             characterNA = "",
             colnamesStyle = cs3)
addDataFrame(Amsterdam_CO2_BelowDL,
             sheet = sheet1,
             startRow = (5 + nrow(Amsterdam_CO2)),
             row.names = FALSE,
             showNA = FALSE,
             characterNA = "",
             colnamesStyle = cs3)
saveWorkbook(wb, "Amsterdam.xlsx")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM