Skip to content

Commit

Permalink
v3.5 issue #56 switch to "none" approach for incorp NA problem
Browse files Browse the repository at this point in the history
  • Loading branch information
sashahafner committed Jun 7, 2023
1 parent 8c758db commit bcf86c4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ALFAM2
Type: Package
Title: Model on Ammonia Emission from Field-Applied Manure
Version: 3.4
Version: 3.5
Date: 2023-06-07
Authors@R: c(person("Sasha D.", "Hafner", role = c("aut", "cre"), email = "sasha.hafner@bce.au.dk"),
person("Christoph", "Haeni", role = "aut"),
Expand Down
6 changes: 6 additions & 0 deletions R/prepDat.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ prepDat <- function(dat, app.mthd.name = 'app.mthd', incorp.name = 'incorp', sou
if (incorp.name %in% names(dat)) {
dat[, incorp.name] <- tolower(dat[, incorp.name])

# Replace NA values with 'none'
if (any(is.na(dat[, incorp.name]))) {
warning(paste0('Some NA values in incorporation column ', incorp.name, '.\nReplacing all with "none".'))
dat[is.na(dat[, incorp.name]), incorp.name] <- 'none'
}

# Convert incorporation values to standards
for (i in 1:length(incorp.levels)) {
dat[dat[, incorp.name] %in% incorp.levels[[i]], incorp.name] <- names(incorp.levels)[i]
Expand Down
9 changes: 0 additions & 9 deletions R/prepIncorp.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ prepIncorp <- function(dat, pars, time.name, time.incorp, incorp.names, warn) {
# Do they exist?
inc.ex <- intersect(inc.names, names(dat))

# Set NAs in incorporation time to Inf so they are skipped
# NTS: was this really not solved before now? Just noticed problem with v3.2!
dat[is.na(dat[, time.incorp]), time.incorp] <- Inf

# Get times and types
if(is.numeric(time.incorp)){
# Unique groups
Expand All @@ -29,11 +25,6 @@ prepIncorp <- function(dat, pars, time.name, time.incorp, incorp.names, warn) {
n.incorp.vals <- rowSums(dat[, inc.ex, drop = FALSE])
n.incorp.vals.grp <- tapply(n.incorp.vals, dat$`__group`, "[", 1)

# Sort out missing values
# NTS: was this really not solved before now? Just noticed problem with v3.2!
n.incorp.vals <- na.omit(n.incorp.vals)
n.incorp.vals.grp[is.na(n.incorp.vals.grp)] <- 0

# If multiple incoporation dummy variables are 1 for any row, throw error
if (any(n.incorp.vals > 1)) {
stop('Multiple incorporation types specified in the same row.\nYou cannot do this!')
Expand Down

0 comments on commit bcf86c4

Please sign in to comment.