Skip to content

Commit

Permalink
Updated NA tests
Browse files Browse the repository at this point in the history
Fixed NA tests code
  • Loading branch information
knoiva-indecon committed Feb 21, 2024
1 parent 4fada0f commit c270fd4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
15 changes: 8 additions & 7 deletions FrEDI/testing/configTests_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@
# }

## Function to check if column has at least one non NA value
has_nonNA_values_df <- function(x, groups0="sector", col0="annual_impacts") {
has_nonNA_values_df <- function(df0, groups0="sector", col0="annual_impacts") {
# ### Check whether values in x are NA
# x <- x |> is.na()
# ### Calculate number of rows
# y <- tibble(numRows = x |> nrow())
# ### Number of NA values
# y <- y |> mutate(numNA = x |> colSums() |> nrow() |> is.null() |> if_else(., 0, 1))
### Add counters
x <- x |> mutate(numRows = 1)
x <- x |> mutate(numNA = 1 * (x[[col0]] |> is.na()))
df0 <- df0 |> mutate(numRows = 1)
vals0 <- df0[[col0]] |> is.na()
x <- df0 |> mutate(numNA = 1 * vals0)
### Summarize
sum0 <- c("numRows", "numNA")
y <- x |> group_by_at(c(groups0)) |> summarize_at(c(sum0), sum)
sum0 <- c("numRows", "numNA")
y <- x |> group_by_at(c(groups0)) |> summarize_at(c(sum0), sum)
### Whether all results are missing
y <- y |> mutate(allNA = (numRows == numNA))
y <- y |> mutate(allNA = (numRows == numNA))
### Filter to values with allNA
z <- y |> filter(allNA)
z <- y |> filter(allNA)
# ### Get number of rows |>
# z <- y |> nrow()
# # z <- 1 * (z > 0)
Expand Down
13 changes: 7 additions & 6 deletions FrEDI/testing/testing_general_fredi_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ general_fredi_test <- function(
#### Check if each sector has at least one non-NA-value

# Get names of sector "group-names" consistent with dplyr
sect_names <- newOutputs |> pull(sector) |> unique()
# sect_names <- newOutputs |> pull(sector) |> unique()

# For each sector group, check if each sector has nonNA values
sect_na_check <- newOutputs |>
group_by_at(c("sector")) |>
has_nonNA_values_df(groups0="sector", col0="annual_impacts")
# group_map(~ has_nonNA_values_df(., col0="annual_impacts")) |>
# set_names(sect_names$sector)
# sect_na_check <- newOutputs |>
# group_by_at(c("sector")) |>
# group_map(~ has_nonNA_values_df(., col0="annual_impacts")) |>
# set_names(sect_names$sector)
sect_na_check <- newOutputs |> has_nonNA_values_df(groups0="sector", col0="annual_impacts")


# If there are any sector groups with all NA values, filter to that sector and save to output list
# sect_na_check$`ATS Extreme Temperature` <- 1
Expand Down

0 comments on commit c270fd4

Please sign in to comment.