[adegenet-commits] r259 - in pkg: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jan 27 20:35:57 CET 2009
Author: jombart
Date: 2009-01-27 20:35:57 +0100 (Tue, 27 Jan 2009)
New Revision: 259
Modified:
pkg/R/genind2genpop.R
pkg/man/genind2genpop.Rd
Log:
genind2genpop can now proceed the other slot, and tapply it by pop (whatever action is requested).
Modified: pkg/R/genind2genpop.R
===================================================================
--- pkg/R/genind2genpop.R 2009-01-27 19:05:05 UTC (rev 258)
+++ pkg/R/genind2genpop.R 2009-01-27 19:35:57 UTC (rev 259)
@@ -1,7 +1,8 @@
#########################
# Function genind2genpop
#########################
-genind2genpop <- function(x,pop=NULL,missing=c("NA","0","chi2"),quiet=FALSE){
+genind2genpop <- function(x,pop=NULL,missing=c("NA","0","chi2"),quiet=FALSE,
+ proceed.other=FALSE, other.action=mean){
if(!is.genind(x)) stop("x is not a valid genind object")
checkType(x)
@@ -60,8 +61,29 @@
prevcall <- match.call()
res <- genpop(tab=tabcount, prevcall=prevcall, ploidy=x at ploidy, type=x at type)
+
+ ## handle @other here
res at other <- x at other
+ if(proceed.other){
+ ## auxiliary function doing the job
+ f1 <- function(e){
+ N <- row(x at tab)
+ if(is.vector(e) & is.numeric(e) & length(e)==N){ # numeric vector
+ res <- tapply(e, pop, other.action)
+ return(res)
+ } else if(is.matrix(e) & is.numeric(e) & nrow(e)==N){ # numeric matrix
+ res <- apply(e, 2, function(vec) tapply(vec, pop, other.action))
+ return(res)
+ } else if(is.data.frame(e) & nrow(e)==N & all(sapply(e,is.numeric)) ){ # df of numeric vectors
+ res <- lapply(e, function(vec) tapply(vec, pop, other.action))
+ res <- data.frame(res)
+ return(res)
+ } else return(e)
+ } # end f1
+ res at other <- lapply(res at other, f1)
+ } # end if(proceed.other)
+
if(missing != "NA"){
res <- na.replace(res, method=missing, quiet=quiet)
}
Modified: pkg/man/genind2genpop.Rd
===================================================================
--- pkg/man/genind2genpop.Rd 2009-01-27 19:05:05 UTC (rev 258)
+++ pkg/man/genind2genpop.Rd 2009-01-27 19:35:57 UTC (rev 259)
@@ -5,7 +5,8 @@
\description{The function \code{genind2genpop} converts genotypes data
(genind) into alleles counts per population (genpop).}
\usage{
-genind2genpop(x,pop=NULL,missing=c("NA","0","chi2"),quiet=FALSE)
+genind2genpop(x,pop=NULL,missing=c("NA","0","chi2"),quiet=FALSE,
+ proceed.other=FALSE, other.action=mean)
}
\arguments{
\item{x}{an object of class \code{genind}.}
@@ -15,9 +16,14 @@
\item{missing}{can be "NA", "0", or "chi2". See details for more information.}
\item{quiet}{logical stating whether a conversion message must be
printed (TRUE,default) or not (FALSE).}
-
+ \item{proceed.other}{a logical indicating whether the \code{@other}
+ slot should be proceeded (see details).}
+ \item{other.action}{a function to be used when proceeding the
+ \code{@other} slot. By default, 'mean' is used.}
}
-\details{The values of the 'missing' argument in \code{genind2genpop}
+\details{
+ === 'missing' argument ===\cr
+ The values of the 'missing' argument in \code{genind2genpop}
have the following effects:\cr
- "NA": if all genotypes of a population for a given allele are
missing, count value will be NA\cr
@@ -28,7 +34,17 @@
- "chi2": if all genotypes of a population for a given allele are
missing, count value will be that of a theoretical count in of a
Chi-squared test. This is obtained by the product of the margins
- sums divided by the total number of alleles.
+ sums divided by the total number of alleles.\cr
+
+ === proceeding the \code{@other} slot ===\cr
+ Essentially, \code{genind2genpop} is about aggregating data per
+ population. The function can do the same for all numeric items in the
+ \code{@other} slot provided they have the same length (for vectors) or
+ the same number of rows (matrix-like objects) as the number of
+ genotypes. When the case is encountered and if \code{proceed.other} is
+ TRUE, then these objects are processed using the function defined in
+ \code{other.action} per population. For instance, spatial coordinates
+ of genotypes would be averaged to obtain population coordinates.
}
\value{
A genpop object. The component @other in 'x' is passed to the created
More information about the adegenet-commits
mailing list