Home > Enterprise >  Function in R to Identify Out of Specification Values
Function in R to Identify Out of Specification Values

Time:09-17

I have the following dataframe called data:

enter image description here

I am trying to create a function which determines out of specification values of "Assayperc" as follows:

is_oos <- function(x){
  return(x < (data$Assayperc < 0.90 && data$Assayperc > 1.10))
}

In other words out of specifiation are Assayperc values less than 90% or greater than 110%.

I then want to use the "is_oos" function to add a out of specificatio column to the data frame as follows:

data <- data %>%
  group_by(Manufacturer) %>%
  mutate(OutOfSpec = ifelse(is_oos(Assayperc), BatchNumber, ""))

The output is as follows which is not as expected as all values are being labelled as out of specification:

enter image description here

Kindly assist with the function.

Regards Chris

CodePudding user response:

In other words out of specifiation are Assayperc values less than 90% or greater than 110%.

As pointed out by Robert, you need | rather than &&.

data <- data %>%
  group_by(Manufacturer) %>%
  mutate(OutOfSpec = ifelse(Assayperc < 0.90 | Assayperc > 1.10, BatchNumber, ""))

Please update the question in line with the comments, it is very difficult to read as is.


Just to add an alternative, you could do this with data.table as follows, which would update by reference. I use fifelse below (fast ifelse).

library(data.table)
setDT(data)

data[, OutOfSpec := fifelse(Assayperc < 0.90 | Assayperc > 1.10, BatchNumber, ""),
 by = .(Manufacturer)]

CodePudding user response:

Your is_oos function does something weird: you first compute a logical value based on the vector data$Assayperc, and then you compare that logical value to the argument x.

What you probably intended was to compute a logical value based on the argument x instead:

is_oos <- function (x) {
  x < 0.90 | x > 1.10
}

Note that I’ve also replaced && with | as noted in the other answer (and it needs to be | rather than || since we need the computation to be vectorised). I also removed the return() call, since it’s misleading clutter.

Your usage of this function is essentially correct, but note that group_by has no effect here and is therefore unnecessary:

data <- data %>%
  mutate(OutOfSpec = ifelse(is_oos(Assayperc), BatchNumber, ""))
  • Related