Search code examples
rvalidationverification

R - Verifying That A Column Only Has Certain Values


I am trying to figure out how to verify that that columns of a spreadsheet only contain a predetermined set of values. Any thoughts on the best way to do this?

To provide an example, I am using the following method to determine that the number of columns is correct.

if(!ncol(data_read) == 9){
    print("There are not 9 columns in this file")
    return(FALSE)
} else if{
  OTHER PARAMETERS HERE (such as the one I'm trying to figure out)
  }

   return(TRUE}
 }

Solution

  • valid_values <- c(1, 2, 3)
    df <- data.frame(a = c(2, 3, 2), b = c(2, 3, 4))
    
    all(df$a %in% valid_values)
    [1] TRUE
    all(df$b %in% valid_values)
    [1] FALSE