homework2.R

my_function = function(x,y,type = "shared"){
    count = 0
    same = character()
    tryCatch(
        if(class(x) == class(y)){
            for(i in x){
                for (j in y){
                    if(i == j){
                        if(!(is.element(i,same))){
                            same = append(same, i) 
                            count = count + 1}
                        else{print ("class error")}
                    }
                }
            }
        },
        error = function(e) print("error at alleles"), 
        warning = function(w) print("warning at alleles"), finally = NULL)
    if(type == "shared"){
        return(count)
    }else if(type == "different"){
        return(length(x) - count)
    }else{
        return("error at type")
    }            
}
my_function(c("A","C"),c("A","C") ,type="shared")  # It should return 2

my_function(c("A","C"),c("A","C") ,type="different")  # It should return 0

my_function(c("G","C"),c("A","C") ,type="shared")  #It should return 1

my_function(c("G","C"),c("A","C") ,type="different") #It should return 1

my_function(c("A","A"),c("A","C") ,type="shared") #It should return 1

my_function(c("A","A"),c("A","C") ,type="different") #It should return 1

my_function(c("A","T"),c("G","C") ,type="different") #It should return 2

my_function(c("A","T"),c(2,1) ,type="different") #It returns "class error" 

my_function(c(NA),c(NA) ,type="different") #It returns "error at alleles"

my_function(c(NA),c(NA) ,type="dd") #It returns "error at alleles"
faarseer/systems_biology documentation built on May 20, 2019, 5:20 p.m.