R/import-data-function.R

Verify <- function(x, s.names=NULL){
  #Check list length for number of objects
  if(length(x) == 3){
    #Store the list objects individually to check each one
    k <- x[[1]]
    m <- x[[2]]
    b <- x[[3]]
    #Check if the list objects are matrices
    if(is.matrix(k) != TRUE | is.matrix(m) != TRUE | is.matrix(b) != TRUE){
      stop("Data is not a matrix")
    }
    #Check that the matrices have the same number of rows
    if(nrow(k) != nrow(m) | nrow(k) != nrow(b)){
      stop("One or more matrices has a different number of rows")
    }
    #Check that the matrices have the same number of columns
    if(ncol(k) != ncol(m) | ncol(k) != ncol(b)){
      stop("One or more matrices has a different number of columns")
    }
    count <- (1:length(s.names)) #Creat a count for the for loop to iterate through the check for names
    #If the default value of s.names is left, this is skipped
    if(is.null(s.names) != TRUE){
      #Otherwise, check that the name vector has the right number of names
      if(nrow(k) != length(s.names)){
      stop("Sample names vector or data has wrong number of row elements")
      }
      #Using the count made earlier, iterate through each column and row name to make sure they match
      for(i in count){
        if(colnames(k)[i] != s.names[i]){
          stop("Column and row names will not match up")
        }
      }
      #Assign row names
      rownames(k) <- s.names
      rownames(m) <- s.names
      rownames(b) <- s.names
    }
    #Check that diagonals are 0, which they should be if everything else is aligned correctly
    for(i in count){
      if(k[i,i] != 0 | m[i,i] != 0 | b[i,i] != 0){
        stop("Diagonals are non-zero")
      }
    }
    #Output the list back in the same order with everything checked
    results <- list(k,m,b)
    return(results)
  }
  #Check list length for number of objects
  if(length(x) == 2){
    #Store list objects separately for check
    k <- x[[1]]
    m <- x[[2]]
    #Check that list objects are matrices
    if(is.matrix(k) != TRUE | is.matrix(m) != TRUE){
      stop("Data is not a matrix")
    }
    #Check that matrices have the same number of rows
    if(nrow(k) != nrow(m)){
      stop("One or more matrices has a different number of rows")
    }
    #Check that matrices have the same number of columns
    if(ncol(k) != ncol(m)){
      stop("One or more matrices has a different number of columns")
    }
    #Create a count for the for loop check of names
    count <- (1:length(s.names))
    #If s.names is left as default, this is skipped
    if(is.null(s.names) != TRUE){
      #Check that the length of the names vector matches the number of rows
      if(nrow(k) != length(s.names)){
        stop("Sample names vector or data has wrong number of row elements")
      }
      #Check that the names are the same as for the columns
      for(i in count){
        if(colnames(k)[i] != s.names[i]){
          stop("Column and row names will not match up")
        }
      }
      #Assign row names
      rownames(k) <- s.names
      rownames(m) <- s.names
    }
    #Check that diagonals are 0, which they should be if everything else is assigned correctly
    for(i in count){
      if(k[i,i] != 0 | m[i,i] != 0){
        stop("Diagonals are non-zero")
      }
    }
    #Store matrices back in a list in the same order
    results <- list(k,m)
    return(results)
  }else{
    #If the list has too many objects
    stop("List contains incorrect data or too many elements")
  }
}
kyarlagadda/499-project documentation built on May 13, 2019, 6:15 p.m.