R/setAs.R

Defines functions as.ktab.genpop as.ktab.genind as.genpop.genind as.matrix.genpop as.matrix.genind as.data.frame.genpop as.data.frame.genind

Documented in as.data.frame.genind as.data.frame.genpop as.genpop.genind as.ktab.genind as.ktab.genpop as.matrix.genind as.matrix.genpop

#############
# S4 methods
#############
setAs("genind", "data.frame", function(from, to) {
    return(as.data.frame(from@tab))
})



setAs("genpop", "data.frame", function(from, to) {
    return(as.data.frame(from@tab))
})



setAs("genind", "matrix", function(from, to) {
    return(from@tab)
})



setAs("genpop", "matrix", function(from, to) {
    return(from@tab)
})



setAs("genind", "genpop", function(from, to) {
    if(!is.genind(from)) stop("object is not a valid genind")

    x <- genind2genpop(from, quiet=TRUE)
    warning("You had better use genind2genpop to specify treatment of NAs")

    return(x@tab)
})




setOldClass("ktab")
setAs("genind", "ktab", function(from, to) {
    checkType(from)
    res <- ktab.data.frame(df=as.data.frame(from), blocks=from@loc.n.all, rownames=indNames(from),
                           colnames=unlist(alleles(from)), tabnames=locNames(from))
    return(res)
})




setAs("genpop", "ktab", function(from, to) {
    checkType(from)
    res <- ktab.data.frame(df=as.data.frame(from), blocks=from@loc.n.all, rownames=popNames(from),
                           colnames=unlist(alleles(from)), tabnames=locNames(from))
    return(res)
})




##############
# S3 versions
##############

#' @export
as.data.frame.genind <- function(x,...){
    return(as.data.frame(tab(x, ...)))
}



#' @export
as.data.frame.genpop <- function(x,...){
    return(as.data.frame(tab(x, ...)))
}



#' @method as.matrix genind
#' @export
as.matrix.genind <- function(x,...){
    return(tab(x, ...))
}



#' @method as.matrix genpop
#' @export
as.matrix.genpop <- function(x,...){
    return(tab(x, ...))
}



#' @method as.genpop genind
#' @export
as.genpop.genind <- function(x,...){
    return(as(x,"genpop"))
}




as.ktab.genind <- function(x,...){
    return(as(x,"ktab"))
}




as.ktab.genpop <- function(x,...){
    return(as(x,"ktab"))
}

Try the adegenet package in your browser

Any scripts or data that you put into this service are public.

adegenet documentation built on Feb. 16, 2023, 6 p.m.