R Under development (unstable) (2024-05-17 r86566 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > #devtools::install_github("psolymos/mefa4") > ## examples > > ## load library > library(mefa) mefa 3.2-9 2024-05-19 > library(mefa4) Loading required package: Matrix mefa4 0.3-10 2024-05-19 Attaching package: 'mefa4' The following objects are masked from 'package:mefa': samp, taxa, xtab > > ## run examples with \dontrun sections > > help_pages <- c("abmibirds", + "find_max", + "groupSums", "mbind", "Mefa", + "Melt", + "nameAlnum", "nonDuplicated", + "%notin%", "r2rmd", + "samp", "Xtab") > > for (i in help_pages) { + cat("\n\n---------- mefa4 example:", i, "----------\n\n") + eval(parse(text=paste0("example('", i, + "', package = 'mefa4', run.dontrun = TRUE)"))) + } ---------- mefa4 example: abmibirds ---------- abmbrd> data(abmibirds) abmbrd> str(abmibirds) 'data.frame': 59341 obs. of 21 variables: $ Rotation : Factor w/ 2 levels "Prototype","Rotation 1": 1 1 1 1 1 1 1 1 1 1 ... $ ABMI.Site : int 630 630 630 630 630 630 630 630 630 630 ... $ Year : int 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 ... $ Field.Date : Factor w/ 132 levels "01-Jun-03","01-Jun-04",..: 46 46 46 46 46 46 46 46 46 46 ... $ Field.Crew.Members : Factor w/ 67 levels "ABL","ABL/RSW",..: 15 15 15 15 15 15 15 15 15 15 ... $ Identification.Date : Factor w/ 135 levels "01-Feb-10","01-Jun-08",..: 135 135 135 135 135 135 135 135 135 135 ... $ Identification.Analyst : Factor w/ 13 levels "CF/CS/TH","CLS/MBI",..: 11 11 11 11 11 11 11 11 11 11 ... $ Point.Count.Station : int 1 1 1 1 1 1 1 1 2 2 ... $ Wind.Conditions : Factor w/ 7 levels "0","1","2","3",..: 4 4 4 4 4 4 4 4 4 4 ... $ Precipitation : Factor w/ 5 levels "DNC","Drizzle",..: 4 4 4 4 4 4 4 4 4 4 ... $ Start.of.Point.Count : Factor w/ 434 levels "10:00","10:01",..: 152 152 152 152 152 152 152 152 178 178 ... $ End.of.Point.Count : Factor w/ 42 levels "10:07","10:21",..: 42 42 42 42 42 42 42 42 42 42 ... $ Common.Name : Factor w/ 217 levels "Accipiters","Alder Flycatcher",..: 184 184 142 142 142 150 208 161 208 181 ... $ Scientific.Name : Factor w/ 218 levels "Accipiter","Accipiter gentilis",..: 207 207 173 173 173 209 201 149 201 57 ... $ Taxonomic.Resolution : Factor w/ 5 levels "Family","Genus",..: 3 3 3 3 3 3 3 3 3 3 ... $ Unique.Taxonomic.Identification.Number: Factor w/ 215 levels "174469","174479",..: 130 130 214 214 214 149 122 161 122 195 ... $ Time.First.Detected : Factor w/ 129 levels ".1",".2",".3",..: 87 97 112 87 31 40 13 68 31 13 ... $ Interval.1 : Factor w/ 6 levels "","0","1","DNC",..: 5 5 5 5 5 5 5 5 5 5 ... $ Interval.2 : Factor w/ 6 levels "","0","1","DNC",..: 5 5 5 5 5 5 5 5 5 5 ... $ Interval.3 : Factor w/ 6 levels "","0","1","DNC",..: 5 5 5 5 5 5 5 5 5 5 ... $ Behaviour : Factor w/ 21 levels "Alarm Calling",..: 19 19 19 19 19 19 19 19 19 19 ... ---------- mefa4 example: find_max ---------- fnd_mx> ## numeric vector fnd_mx> compare_sets(1:10, 8:15) xlength ylength intersect union xbutnoty ybutnotx labels 10 8 3 15 7 5 unique 10 8 3 15 7 5 fnd_mx> ## factor with 'zombie' labels fnd_mx> compare_sets(factor(1:10, levels=1:10), factor(8:15, levels=1:15)) xlength ylength intersect union xbutnoty ybutnotx labels 10 15 10 15 0 5 unique 10 8 3 15 7 5 fnd_mx> (mat <- matrix(rnorm(10*5), 10, 5)) [,1] [,2] [,3] [,4] [,5] [1,] 0.6225481 1.16458131 0.620313023 0.929269721 1.12635931 [2,] -0.4541257 0.39726828 -2.236981617 0.409886539 -0.55887342 [3,] 1.4300601 -0.92897071 2.134706169 -0.306224329 -0.01410747 [4,] 0.2100835 1.58297747 -0.244405702 0.094398887 -0.77112648 [5,] -1.0760561 0.04558613 0.484498461 -0.364954366 -1.04487027 [6,] -0.8421567 -0.44275635 -0.004780766 0.292980463 -0.73616200 [7,] 0.2002153 0.85752277 -1.311813439 0.008584822 0.25554121 [8,] 1.2847212 1.00280994 -0.060925105 0.882223603 -2.12177303 [9,] -0.6582641 0.27330465 -0.477034812 -0.762584092 -0.81534958 [10,] -0.5384761 0.07878689 -1.057297541 0.683688905 -0.03130146 fnd_mx> (m <- find_max(mat)) index value 1 X2 1.1645813 2 X4 0.4098865 3 X3 2.1347062 4 X2 1.5829775 5 X3 0.4844985 6 X4 0.2929805 7 X2 0.8575228 8 X1 1.2847212 9 X2 0.2733046 10 X4 0.6836889 fnd_mx> ## column indices fnd_mx> as.integer(m$index) [1] 2 4 3 2 3 4 2 1 2 4 fnd_mx> find_min(mat) index value 1 X3 0.6203130 2 X3 -2.2369816 3 X2 -0.9289707 4 X5 -0.7711265 5 X1 -1.0760561 6 X1 -0.8421567 7 X3 -1.3118134 8 X5 -2.1217730 9 X5 -0.8153496 10 X3 -1.0572975 fnd_mx> map <- cbind(c("a","b","c","d","e","f","g"), fnd_mx+ c("A","B","B","C","D","D","E")) fnd_mx> #x <- factor(sample(map[1:6,1], 100, replace=TRUE), levels=map[,1]) fnd_mx> x <- as.factor(sample(map[1:6,1], 100, replace=TRUE)) fnd_mx> x[2] <- NA fnd_mx> table(x, reclass(x, map, all = FALSE), useNA="always") x A B C D a 15 0 0 0 0 b 0 14 0 0 0 c 0 15 0 0 0 d 0 0 21 0 0 e 0 0 0 14 0 f 0 0 0 20 0 0 0 0 0 1 fnd_mx> table(x, reclass(x, map, all = TRUE), useNA="always") x A B C D E a 15 0 0 0 0 0 b 0 14 0 0 0 0 c 0 15 0 0 0 0 d 0 0 21 0 0 0 e 0 0 0 14 0 0 f 0 0 0 20 0 0 0 0 0 0 0 1 fnd_mx> map[c(4, 7), 2] <- NA fnd_mx> table(x, reclass(x, map, all = FALSE, allow_NA = TRUE), useNA="always") x A B D a 15 0 0 0 b 0 14 0 0 c 0 15 0 0 d 0 0 0 21 e 0 0 14 0 f 0 0 20 0 0 0 0 1 fnd_mx> table(x, reclass(x, map, all = TRUE, allow_NA = TRUE), useNA="always") x A B D a 15 0 0 0 b 0 14 0 0 c 0 15 0 0 d 0 0 0 21 e 0 0 14 0 f 0 0 20 0 0 0 0 1 fnd_mx> (mat2 <- exp(mat) / rowSums(exp(mat))) [,1] [,2] [,3] [,4] [,5] [1,] 0.14856090 0.25545038 0.14822922 0.2018887 0.24587079 [2,] 0.14739956 0.34534399 0.02478630 0.3497292 0.13274092 [3,] 0.28330550 0.02677571 0.57316401 0.0499110 0.06684378 [4,] 0.14604635 0.57640929 0.09270615 0.1300917 0.05474653 [5,] 0.08403882 0.25799005 0.40014755 0.1711226 0.08670093 [6,] 0.11080770 0.16520657 0.25599867 0.3447893 0.12319778 [7,] 0.19870646 0.38342197 0.04380728 0.1640544 0.21000988 [8,] 0.36811841 0.27768667 0.09584750 0.2461416 0.01220579 [9,] 0.15401803 0.39097298 0.18461987 0.1387605 0.13162859 [10,] 0.11758909 0.21799219 0.06999157 0.3991596 0.19526759 fnd_mx> (rmat2 <- redistribute(mat2, source = 1, target = 2:4)) [,1] [,2] [,3] [,4] [,5] [1,] 0 0.3181187 0.18459352 0.25141700 0.24587079 [2,] 0 0.4160572 0.02986158 0.42134034 0.13274092 [3,] 0 0.0384487 0.82303758 0.07166994 0.06684378 [4,] 0 0.6817418 0.10964718 0.15386451 0.05474653 [5,] 0 0.2841353 0.44069927 0.18846454 0.08670093 [6,] 0 0.1891051 0.29303108 0.39466602 0.12319778 [7,] 0 0.5122745 0.05852912 0.21918645 0.21000988 [8,] 0 0.4426464 0.15278571 0.39236206 0.01220579 [9,] 0 0.4752686 0.22442481 0.16867798 0.13162859 [10,] 0 0.2552966 0.08196905 0.46746672 0.19526759 fnd_mx> colMeans(mat2) [1] 0.1758591 0.2897250 0.1889298 0.2195649 0.1259213 fnd_mx> colMeans(rmat2) [1] 0.0000000 0.3613093 0.2398579 0.2729116 0.1259213 fnd_mx> stopifnot(abs(sum(mat2) - sum(rmat2)) < 10^-6) ---------- mefa4 example: groupSums ---------- grpSms> x <- data.frame( grpSms+ sample = paste("Sample", c(1,1,2,2,3,4), sep="."), grpSms+ species = c(paste("Species", c(1,1,1,2,3), sep="."), grpSms+ "zero.pseudo"), count = c(1,2,10,3,4,0), grpSms+ stringsAsFactors = TRUE) grpSms> samp <- data.frame(samples=levels(x$sample), var1=1:2, grpSms+ stringsAsFactors = TRUE) grpSms> taxa <- data.frame(specnames=levels(x$species), var2=c("b","a"), grpSms+ stringsAsFactors = TRUE) grpSms> rownames(samp) <- samp$samples grpSms> rownames(taxa) <- taxa$specnames grpSms> x2 <- Xtab(count ~ sample + species, x, cdrop=FALSE,rdrop=TRUE) grpSms> x5 <- Mefa(x2, samp, taxa, join="inner") grpSms> groupSums(as.matrix(x2), 1, c(1,1,2)) Species.1 Species.2 Species.3 zero.pseudo 1 13 3 0 0 2 0 0 4 0 grpSms> groupSums(as.matrix(x2), 2, c(1,1,2,2)) 1 2 Sample.1 3 0 Sample.2 13 0 Sample.3 0 4 grpSms> groupSums(x2, 1, c(1,1,2)) 2 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo 1 13 3 . . 2 . . 4 . grpSms> groupSums(x2, 2, c(1,1,2,2)) 3 x 2 sparse Matrix of class "dgCMatrix" 1 2 Sample.1 3 . Sample.2 13 . Sample.3 . 4 grpSms> groupSums(x5, 1, c(1,1,2)) Object of class "Mefa" ..@ xtab: 2 x 4 sparse Matrix ..@ samp: NULL ..@ taxa: data frame with 2 variables ..@ join: inner grpSms> groupSums(x5, 2, c(1,1,2,2)) Object of class "Mefa" ..@ xtab: 3 x 2 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: NULL ..@ join: inner grpSms> groupMeans(as.matrix(x2), 1, c(1,1,2)) Species.1 Species.2 Species.3 zero.pseudo 1 6.5 1.5 0 0 2 0.0 0.0 4 0 grpSms> groupMeans(as.matrix(x2), 2, c(1,1,2,2)) 1 2 Sample.1 1.5 0 Sample.2 6.5 0 Sample.3 0.0 2 grpSms> groupMeans(x2, 1, c(1,1,2)) 2 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo 1 6.5 1.5 . . 2 . . 4 . grpSms> groupMeans(x2, 2, c(1,1,2,2)) 3 x 2 sparse Matrix of class "dgCMatrix" 1 2 Sample.1 1.5 . Sample.2 6.5 . Sample.3 . 2 grpSms> groupMeans(x5, 1, c(1,1,2)) Object of class "Mefa" ..@ xtab: 2 x 4 sparse Matrix ..@ samp: NULL ..@ taxa: data frame with 2 variables ..@ join: inner grpSms> groupMeans(x5, 2, c(1,1,2,2)) Object of class "Mefa" ..@ xtab: 3 x 2 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: NULL ..@ join: inner grpSms> sum_by(runif(100, 0, 1), sample(LETTERS[1:4], 100, replace=TRUE)) x by A 15.30507 27 C 12.09593 25 B 12.75641 24 D 11.45510 24 ---------- mefa4 example: mbind ---------- mbind> x=matrix(1:4,2,2) mbind> rownames(x) <- c("a","b") mbind> colnames(x) <- c("A","B") mbind> y=matrix(11:14,2,2) mbind> rownames(y) <- c("b","c") mbind> colnames(y) <- c("B","C") mbind> sampx <- data.frame(x1=1:2, x2=2:1, mbind+ stringsAsFactors = TRUE) mbind> rownames(sampx) <- rownames(x) mbind> sampy <- data.frame(x1=3:4, x3=10:11, mbind+ stringsAsFactors = TRUE) mbind> rownames(sampy) <- rownames(y) mbind> taxay <- data.frame(x1=1:2, x2=2:1, mbind+ stringsAsFactors = TRUE) mbind> rownames(taxay) <- colnames(y) mbind> taxax <- NULL mbind> mbind(x,y) A B C a 1 3 NA b 2 4 13 c NA 12 14 mbind> mbind(as(x,"sparseMatrix"),as(y,"sparseMatrix")) 3 x 3 sparse Matrix of class "dgCMatrix" A B C a 1 3 NA b 2 4 13 c NA 12 14 mbind> xy <- mbind(Mefa(x,sampx),Mefa(y,sampy,taxay)) mbind> unclass(xy) attr(,"xtab") 3 x 3 sparse Matrix of class "dgCMatrix" A B C a 1 3 NA b 2 4 13 c NA 12 14 attr(,"samp") x1 x2 x3 a 1 2 NA b 2 1 10 c 4 NA 11 attr(,"taxa") x1 x2 A NA NA B 1 2 C 2 1 attr(,"join") [1] "left" mbind> mbind2(x,y) A B C a 1 3 NA b 2 15 13 c NA 12 14 mbind> mbind2(as(x,"sparseMatrix"),as(y,"sparseMatrix")) 3 x 3 sparse Matrix of class "dgCMatrix" A B C a 1 3 NA b 2 15 13 c NA 12 14 mbind> xtab(xy) <- mbind2(x, y) mbind> unclass(xy) attr(,"xtab") 3 x 3 sparse Matrix of class "dgCMatrix" A B C a 1 3 NA b 2 15 13 c NA 12 14 attr(,"samp") x1 x2 x3 a 1 2 NA b 2 1 10 c 4 NA 11 attr(,"taxa") x1 x2 A NA NA B 1 2 C 2 1 attr(,"join") [1] "left" ---------- mefa4 example: Mefa ---------- Mefa> x <- data.frame( Mefa+ sample = paste("Sample", c(1,1,2,2,3,4), sep="."), Mefa+ species = c(paste("Species", c(1,1,1,2,3), sep="."), "zero.pseudo"), Mefa+ count = c(1,2,10,3,4,0), Mefa+ stringsAsFactors = TRUE) Mefa> samp <- data.frame(samples=levels(x$sample), var1=1:2, Mefa+ stringsAsFactors = TRUE) Mefa> taxa <- data.frame(specnames=levels(x$species), var2=c("b","a"), Mefa+ stringsAsFactors = TRUE) Mefa> rownames(samp) <- samp$samples Mefa> rownames(taxa) <- taxa$specnames Mefa> ## Xtab class, counts by repetitions in RHS Mefa> (x0 <- Xtab(~ sample + species, x)) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 2 . . . Sample.2 1 1 . . Sample.3 . . 1 . Sample.4 . . . 1 Mefa> ## counts by LHS and repetitions in RHS Mefa> (x1 <- Xtab(count ~ sample + species, x)) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . Mefa> ## drop all empty rows Mefa> (x2 <- Xtab(count ~ sample + species, x, cdrop=FALSE,rdrop=TRUE)) 3 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Mefa> ## drop all empty columns Mefa> Xtab(count ~ sample + species, x, cdrop=TRUE,rdrop=FALSE) 4 x 3 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 Sample.1 3 . . Sample.2 10 3 . Sample.3 . . 4 Sample.4 . . . Mefa> ## drop specific columns by placeholder Mefa> Xtab(count ~ sample + species, x, cdrop="zero.pseudo") 4 x 3 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 Sample.1 3 . . Sample.2 10 3 . Sample.3 . . 4 Sample.4 . . . Mefa> ## Mefa class, standard Mefa> (x3 <- Mefa(x1, samp, taxa)) Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left Mefa> unclass(x3) attr(,"xtab") 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 Sample.4 2 attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo zero.pseudo a attr(,"join") [1] "left" Mefa> x3@xtab 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . Mefa> x3@samp samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 Sample.4 2 Mefa> x3@taxa specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo zero.pseudo a Mefa> x3@join [1] "left" Mefa> ## effects of left join, NULL taxa slot, xtab is (not sparse) matrix Mefa> (x4 <- Mefa(as.matrix(x1), samp[1:2,])) Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: NULL ..@ join: left Mefa> unclass(x4) attr(,"xtab") 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 NA Sample.4 NA attr(,"taxa") `\001NULL\001` attr(,"join") [1] "left" Mefa> ## effects of inner join (intersect) Mefa> (x5 <- Mefa(x2, samp, taxa, join="inner")) Object of class "Mefa" ..@ xtab: 3 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: inner Mefa> unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo zero.pseudo a attr(,"join") [1] "inner" Mefa> unclass(Mefa(x1, samp[1:2,], join="inner")) attr(,"xtab") 2 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 attr(,"taxa") `\001NULL\001` attr(,"join") [1] "inner" Mefa> ## xtab only Mefa Mefa> (x6 <- Mefa(x1)) Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: NULL ..@ taxa: NULL ..@ join: left Mefa> ## creating new Mefa object without Mefa() Mefa> new("Mefa", xtab=x1, samp=samp, taxa=taxa,join="left") Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left Mefa> ## dim and dimnames Mefa> dim(x5) [1] 3 4 Mefa> dimnames(x5) [[1]] [1] "Sample.1" "Sample.2" "Sample.3" [[2]] [1] "Species.1" "Species.2" "Species.3" "zero.pseudo" Mefa> dn <- list(paste("S", 1:3, sep=""), paste("SPP", 1:4, sep="")) Mefa> dimnames(x5) <- dn Mefa> unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" SPP1 SPP2 SPP3 SPP4 S1 3 . . . S2 10 3 . . S3 . . 4 . attr(,"samp") samples var1 S1 Sample.1 1 S2 Sample.2 2 S3 Sample.3 1 attr(,"taxa") specnames var2 SPP1 Species.1 b SPP2 Species.2 a SPP3 Species.3 b SPP4 zero.pseudo a attr(,"join") [1] "inner" Mefa> dimnames(x5)[[1]] <- paste("S", 1:3, sep="_") Mefa> unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" SPP1 SPP2 SPP3 SPP4 S_1 3 . . . S_2 10 3 . . S_3 . . 4 . attr(,"samp") samples var1 S_1 Sample.1 1 S_2 Sample.2 2 S_3 Sample.3 1 attr(,"taxa") specnames var2 SPP1 Species.1 b SPP2 Species.2 a SPP3 Species.3 b SPP4 zero.pseudo a attr(,"join") [1] "inner" Mefa> dimnames(x5)[[2]] <- paste("SPP", 1:4, sep="_") Mefa> unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" SPP_1 SPP_2 SPP_3 SPP_4 S_1 3 . . . S_2 10 3 . . S_3 . . 4 . attr(,"samp") samples var1 S_1 Sample.1 1 S_2 Sample.2 2 S_3 Sample.3 1 attr(,"taxa") specnames var2 SPP_1 Species.1 b SPP_2 Species.2 a SPP_3 Species.3 b SPP_4 zero.pseudo a attr(,"join") [1] "inner" Mefa> ## transpose Mefa> x5 Object of class "Mefa" ..@ xtab: 3 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: inner Mefa> t(x5) Object of class "Mefa" ..@ xtab: 4 x 3 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: inner Mefa> unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" SPP_1 SPP_2 SPP_3 SPP_4 S_1 3 . . . S_2 10 3 . . S_3 . . 4 . attr(,"samp") samples var1 S_1 Sample.1 1 S_2 Sample.2 2 S_3 Sample.3 1 attr(,"taxa") specnames var2 SPP_1 Species.1 b SPP_2 Species.2 a SPP_3 Species.3 b SPP_4 zero.pseudo a attr(,"join") [1] "inner" Mefa> unclass(t(x5)) attr(,"xtab") 4 x 3 sparse Matrix of class "dgCMatrix" S_1 S_2 S_3 SPP_1 3 10 . SPP_2 . 3 . SPP_3 . . 4 SPP_4 . . . attr(,"samp") specnames var2 SPP_1 Species.1 b SPP_2 Species.2 a SPP_3 Species.3 b SPP_4 zero.pseudo a attr(,"taxa") samples var1 S_1 Sample.1 1 S_2 Sample.2 2 S_3 Sample.3 1 attr(,"join") [1] "inner" Mefa> ## 0 and 1 row/col Mefa object Mefa> x3[c(FALSE,FALSE,FALSE,FALSE),c(FALSE,FALSE,FALSE,FALSE)] Object of class "Mefa" ..@ xtab: 0 x 0 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left Mefa> x3[c(TRUE,FALSE,FALSE,FALSE),c(FALSE,FALSE,FALSE,FALSE)] Object of class "Mefa" ..@ xtab: 1 x 0 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left Mefa> x3[c(FALSE,FALSE,FALSE,FALSE),c(TRUE,FALSE,FALSE,FALSE)] Object of class "Mefa" ..@ xtab: 0 x 1 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left Mefa> x3[c(TRUE,FALSE,FALSE,FALSE),c(TRUE,FALSE,FALSE,FALSE)] Object of class "Mefa" ..@ xtab: 1 x 1 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left Mefa> ## stack Mefa> stack(x3) samp taxa values samp_samples samp_var1 taxa_specnames Sample.1 Sample.1 Species.1 3 Sample.1 1 Species.1 Sample.2 Sample.2 Species.1 10 Sample.2 2 Species.1 Sample.3 Sample.3 Species.1 0 Sample.3 1 Species.1 Sample.4 Sample.4 Species.1 0 Sample.4 2 Species.1 Sample.1.1 Sample.1 Species.2 0 Sample.1 1 Species.2 Sample.2.1 Sample.2 Species.2 3 Sample.2 2 Species.2 Sample.3.1 Sample.3 Species.2 0 Sample.3 1 Species.2 Sample.4.1 Sample.4 Species.2 0 Sample.4 2 Species.2 Sample.1.2 Sample.1 Species.3 0 Sample.1 1 Species.3 Sample.2.2 Sample.2 Species.3 0 Sample.2 2 Species.3 Sample.3.2 Sample.3 Species.3 4 Sample.3 1 Species.3 Sample.4.2 Sample.4 Species.3 0 Sample.4 2 Species.3 Sample.1.3 Sample.1 zero.pseudo 0 Sample.1 1 zero.pseudo Sample.2.3 Sample.2 zero.pseudo 0 Sample.2 2 zero.pseudo Sample.3.3 Sample.3 zero.pseudo 0 Sample.3 1 zero.pseudo Sample.4.3 Sample.4 zero.pseudo 0 Sample.4 2 zero.pseudo taxa_var2 Sample.1 b Sample.2 b Sample.3 b Sample.4 b Sample.1.1 a Sample.2.1 a Sample.3.1 a Sample.4.1 a Sample.1.2 b Sample.2.2 b Sample.3.2 b Sample.4.2 b Sample.1.3 a Sample.2.3 a Sample.3.3 a Sample.4.3 a ---------- mefa4 example: Melt ---------- Melt> xx <- data.frame( Melt+ sample = paste("Sample", c(1,1,2,2,3,4), sep="."), Melt+ species = c(paste("Species", c(1,1,1,2,3), sep="."), "zero.pseudo"), Melt+ count = c(1,2,10,3,4,0), Melt+ segment = letters[c(6,13,6,13,6,6)], Melt+ stringsAsFactors = TRUE) Melt> xx sample species count segment 1 Sample.1 Species.1 1 f 2 Sample.1 Species.1 2 m 3 Sample.2 Species.1 10 f 4 Sample.2 Species.2 3 m 5 Sample.3 Species.3 4 f 6 Sample.4 zero.pseudo 0 f Melt> xx0 <- Xtab(count ~ sample + species, xx) Melt> xx1 <- Xtab(count ~ sample + species + segment, xx) Melt> (M1 <- Melt(xx0)) rows cols value 1 Sample.1 Species.1 3 2 Sample.2 Species.1 10 3 Sample.2 Species.2 3 4 Sample.3 Species.3 4 Melt> Melt(as.matrix(xx0)) rows cols value 1 Sample.1 Species.1 3 2 Sample.2 Species.1 10 3 Sample.2 Species.2 3 4 Sample.3 Species.3 4 Melt> (M2 <- Melt(xx1)) rows cols segm value 1 Sample.1 Species.1 f 1 2 Sample.2 Species.1 f 10 3 Sample.3 Species.3 f 4 4 Sample.1 Species.1 m 2 5 Sample.2 Species.2 m 3 Melt> stopifnot(identical(Xtab(value ~ rows + cols, M1), xx0)) Melt> stopifnot(identical(Xtab(value ~ rows + cols + segm, M2), xx1)) ---------- mefa4 example: nameAlnum ---------- nmAlnm> paste0(pasteDate("file", "name", sep="-", sep.date="_"), ".csv") [1] "file-name_2024-05-20.csv" nmAlnm> paste0(paste0date("file", "name", sep.date="_"), ".csv") [1] "filename_2024-05-20.csv" nmAlnm> data(abmibirds) nmAlnm> x <- data.frame(Name=levels(abmibirds[["Common.Name"]]), nmAlnm+ NameAlnum=nameAlnum(levels(abmibirds[["Common.Name"]])), nmAlnm+ stringsAsFactors = TRUE) nmAlnm> x[grep("'", x$Name),] Name NameAlnum 14 Baird's Sparrow Bairds Sparrow 21 Barrow's Goldeneye Barrows Goldeneye 38 Bonaparte's Gull Bonapartes Gull 41 Brewer's Blackbird Brewers Blackbird 42 Brewer's Sparrow Brewers Sparrow 86 Forster's Tern Forsters Tern 88 Franklin's Gull Franklins Gull 103 Hammond's Flycatcher Hammonds Flycatcher 112 Le Conte's Sparrow Le Contes Sparrow 117 Lincoln's Sparrow Lincolns Sparrow 123 Mccown's Longspur Mccowns Longspur 130 Nelson's Sharp-tailed Sparrow Nelsons Sharptailed Sparrow 170 Say's Phoebe Says Phoebe 179 Sprague's Pipit Spragues Pipit 181 Swainson's Thrush Swainsons Thrush 206 Wilson's Phalarope Wilsons Phalarope 207 Wilson's Warbler Wilsons Warbler nmAlnm> data.frame(out=sapply(c("asis", "first", "none", "all", "mixed"), nmAlnm+ function(z) nameAlnum("Olive-sided Flycatcher", z)), nmAlnm+ stringsAsFactors = TRUE) out asis Olivesided Flycatcher first Olivesided Flycatcher none olivesided flycatcher all OLIVESIDED FLYCATCHER mixed Olivesided Flycatcher nmAlnm> z <- data.frame(Name=levels(abmibirds[["Common.Name"]]), nmAlnm+ NameNormalized=normalizeNames(levels(abmibirds[["Common.Name"]]), nmAlnm+ pattern=list("'", "-", " "), replacement=list("", "_", "_")), nmAlnm+ stringsAsFactors = TRUE) nmAlnm> z[grepl("'", z$Name) & grepl("-", z$Name),] Name NameNormalized 130 Nelson's Sharp-tailed Sparrow Nelsons_Sharp_tailed_Sparrow ---------- mefa4 example: nonDuplicated ---------- nnDplc> data(abmibirds) nnDplc> x <- nonDuplicated(abmibirds, abmibirds$ABMI.Site, TRUE) nnDplc> ## or equivalently nnDplc> #x <- nonDuplicated(abmibirds, ABMI.Site, TRUE) nnDplc> dim(abmibirds) [1] 59341 21 nnDplc> dim(x) [1] 330 21 nnDplc> length(unique(abmibirds$ABMI.Site)) [1] 330 ---------- mefa4 example: %notin% ---------- %notn%> 1:10 %notin% c(1,3,5,9) [1] FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE %notn%> sstr <- c("c","ab","B","bba","c",NA,"@","bla","a","Ba","%") %notn%> sstr[sstr %notin% c(letters, LETTERS)] [1] "ab" "bba" NA "@" "bla" "Ba" "%" ---------- mefa4 example: r2rmd ---------- r2rmd> (r2rmd(system.file("r2rmd_example.R", package="mefa4"), r2rmd+ out=NULL, extra=", eval=FALSE")) [1] "---" [2] "title: \"r2rmd function usage example\"" [3] "author: \"Peter Solymos\"" [4] "date: \"May 13, 2015\"" [5] "output: pdf_document" [6] "---" [7] "" [8] "# Rules" [9] "" [10] " * leading `##` is treated as *non-code*" [11] " * leading `#` followed by other than `#` is *code comment*" [12] " * leading `#` after whitespace is *code comment*" [13] " * newline is *code* when preceded and followed by code" [14] "" [15] " The leading double hash `##` trimmed for comment lines." [16] " R markdown chunk start/end stuff is added for code chunks." [17] " The argument `extra` adds chunk arguments, e.g. `extra=', eval=FALSE'` etc." [18] " The function returns vector if `out=NULL`." [19] "" [20] "# Trivial example" [21] "" [22] " Addition" [23] "```{r CHUNK_1, eval=FALSE}\na <- 1" [24] "b <- a + 1 # comment\n```\n" [25] " Function" [26] "```{r CHUNK_2, eval=FALSE}\n# another comment" [27] "f <- function(x) x + a" [28] "f (b)\n```\n" [29] " That's all folks!" [30] "" ---------- mefa4 example: samp ---------- samp> x <- data.frame( samp+ sample = paste("Sample", c(1,1,2,2,3,4), sep="."), samp+ species = c(paste("Species", c(1,1,1,2,3), sep="."), "zero.pseudo"), samp+ count = c(1,2,10,3,4,0), samp+ stringsAsFactors = TRUE) samp> samp <- data.frame(samples=levels(x$sample), var1=1:2, samp+ stringsAsFactors = TRUE) samp> taxa <- data.frame(specnames=levels(x$species), var2=c("b","a"), samp+ stringsAsFactors = TRUE) samp> rownames(samp) <- samp$samples samp> rownames(taxa) <- taxa$specnames samp> x1 <- Xtab(count ~ sample + species, x) samp> x3 <- Mefa(x1, samp, taxa) samp> ## accessing the xtab slot samp> xtab(x3) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . samp> ## replacing the slot value samp> x1[3,1] <- 999 samp> xtab(x3) <- x1 samp> xtab(x3) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 999 . 4 . Sample.4 . . . . samp> ## accessing and replacing the samp slot samp> samp(x3) samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 Sample.4 2 samp> samp(x3) <- NULL samp> samp(x3) NULL samp> samp(x3) <- samp[1:3,] samp> samp(x3) samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 NA samp> ## accessing and replacing the taxa slot samp> taxa(x3) specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo zero.pseudo a samp> taxa(x3) <- NULL samp> taxa(x3) NULL samp> taxa(x3) <- taxa[1:3,] samp> taxa(x3) specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo samp> ## subsetting samp> unclass(x3[3:2, 1:2]) attr(,"xtab") 2 x 2 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Sample.3 999 . Sample.2 10 3 attr(,"samp") samples var1 Sample.3 Sample.3 1 Sample.2 Sample.2 2 attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a attr(,"join") [1] "left" samp> unclass(x3[3:2,]) attr(,"xtab") 2 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.3 999 . 4 . Sample.2 10 3 . . attr(,"samp") samples var1 Sample.3 Sample.3 1 Sample.2 Sample.2 2 attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo attr(,"join") [1] "left" samp> unclass(x3[,1:2]) attr(,"xtab") 4 x 2 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Sample.1 3 . Sample.2 10 3 Sample.3 999 . Sample.4 . . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 NA attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a attr(,"join") [1] "left" ---------- mefa4 example: Xtab ---------- Xtab> x <- data.frame( Xtab+ sample = paste("Sample", c(1,1,2,2,3,4), sep="."), Xtab+ species = c(paste("Species", c(1,1,1,2,3), sep="."), "zero.pseudo"), Xtab+ count = c(1,2,10,3,4,0), Xtab+ stringsAsFactors = TRUE) Xtab> x sample species count 1 Sample.1 Species.1 1 2 Sample.1 Species.1 2 3 Sample.2 Species.1 10 4 Sample.2 Species.2 3 5 Sample.3 Species.3 4 6 Sample.4 zero.pseudo 0 Xtab> ## Xtab class, counts by repetitions in RHS Xtab> (x0 <- Xtab(~ sample + species, x)) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 2 . . . Sample.2 1 1 . . Sample.3 . . 1 . Sample.4 . . . 1 Xtab> ## counts by LHS and repetitions in RHS Xtab> (x1 <- Xtab(count ~ sample + species, x)) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . Xtab> ## drop all empty rows Xtab> (x2 <- Xtab(count ~ sample + species, x, cdrop=FALSE,rdrop=TRUE)) 3 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Xtab> ## drop all empty columns Xtab> Xtab(count ~ sample + species, x, cdrop=TRUE,rdrop=FALSE) 4 x 3 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 Sample.1 3 . . Sample.2 10 3 . Sample.3 . . 4 Sample.4 . . . Xtab> ## drop specific columns by placeholder Xtab> Xtab(count ~ sample + species, x, cdrop="zero.pseudo") 4 x 3 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 Sample.1 3 . . Sample.2 10 3 . Sample.3 . . 4 Sample.4 . . . Xtab> ## 2 and 3 way crosstabs Xtab> xx <- data.frame( Xtab+ sample = paste("Sample", c(1,1,2,2,3,4), sep="."), Xtab+ species = c(paste("Species", c(1,1,1,2,3), sep="."), "zero.pseudo"), Xtab+ count = c(1,2,10,3,4,0), Xtab+ segment = letters[c(6,13,6,13,6,6)], Xtab+ stringsAsFactors = TRUE) Xtab> xx sample species count segment 1 Sample.1 Species.1 1 f 2 Sample.1 Species.1 2 m 3 Sample.2 Species.1 10 f 4 Sample.2 Species.2 3 m 5 Sample.3 Species.3 4 f 6 Sample.4 zero.pseudo 0 f Xtab> Xtab(count ~ sample + species, xx) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . Xtab> Xtab(count ~ sample + species + segment, xx) $f 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 1 . . . Sample.2 10 . . . Sample.3 . . 4 . Sample.4 . . . . $m 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 2 . . . Sample.2 . 3 . . Sample.3 . . . . Sample.4 . . . . > > > ## create input data > x <- data.frame( + sample = paste("Sample", c(1,1,2,2,3,4), sep="."), + species = c(paste("Species", c(1,1,1,2,3), sep="."), "zero.pseudo"), + count = c(1,2,10,3,4,0), + segment = letters[c(6,13,6,13,6,6)], + stringsAsFactors = TRUE) > x sample species count segment 1 Sample.1 Species.1 1 f 2 Sample.1 Species.1 2 m 3 Sample.2 Species.1 10 f 4 Sample.2 Species.2 3 m 5 Sample.3 Species.3 4 f 6 Sample.4 zero.pseudo 0 f > samp <- data.frame(samples=levels(x$sample), var1=1:2, + stringsAsFactors = TRUE) > taxa <- data.frame(specnames=levels(x$species), var2=c("b","a"), + stringsAsFactors = TRUE) > rownames(samp) <- samp$samples > rownames(taxa) <- taxa$specnames > samp samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 Sample.4 2 > taxa specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo zero.pseudo a > ## Xtab class, counts by repetitions in RHS > (x0 <- Xtab(~ sample + species, x)) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 2 . . . Sample.2 1 1 . . Sample.3 . . 1 . Sample.4 . . . 1 > ## counts by LHS and repetitions in RHS > (x1 <- Xtab(count ~ sample + species, x)) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . > ## drop all empty rows > (x2 <- Xtab(count ~ sample + species, x, cdrop=FALSE,rdrop=TRUE)) 3 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . > ## drop all empty columns > Xtab(count ~ sample + species, x, cdrop=TRUE,rdrop=FALSE) 4 x 3 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 Sample.1 3 . . Sample.2 10 3 . Sample.3 . . 4 Sample.4 . . . > ## drop specific columns by placeholder > Xtab(count ~ sample + species, x, cdrop="zero.pseudo") 4 x 3 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 Sample.1 3 . . Sample.2 10 3 . Sample.3 . . 4 Sample.4 . . . > > ## 3-way crosstab > x33 <- Xtab(count ~ sample + species + segment, x) > > ## Mefa class, standard > (x3 <- Mefa(x1, samp, taxa)) Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left > unclass(x3) attr(,"xtab") 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 Sample.4 2 attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo zero.pseudo a attr(,"join") [1] "left" > ## effects of left join, NULL taxa slot, xtab is (not sparse) matrix > (x4 <- Mefa(as.matrix(x1), samp[1:2,])) Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: NULL ..@ join: left > unclass(x4) attr(,"xtab") 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 NA Sample.4 NA attr(,"taxa") `\001NULL\001` attr(,"join") [1] "left" > ## effects of inner join (intersect) > (x5 <- Mefa(x2, samp, taxa, join="inner")) Object of class "Mefa" ..@ xtab: 3 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: inner > unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo zero.pseudo a attr(,"join") [1] "inner" > unclass(Mefa(x1, samp[1:2,], join="inner")) attr(,"xtab") 2 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 attr(,"taxa") `\001NULL\001` attr(,"join") [1] "inner" > ## xtab only Mefa > (x6 <- Mefa(x1)) Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: NULL ..@ taxa: NULL ..@ join: left > ## creating new Mefa object without Mefa() > new("Mefa", xtab=x1, samp=samp, taxa=taxa,join="left") Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left > > ## 0 and 1 row/col Mefa object > x3[c(FALSE,FALSE,FALSE,FALSE),c(FALSE,FALSE,FALSE,FALSE)] Object of class "Mefa" ..@ xtab: 0 x 0 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left > x3[c(TRUE,FALSE,FALSE,FALSE),c(FALSE,FALSE,FALSE,FALSE)] Object of class "Mefa" ..@ xtab: 1 x 0 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left > x3[c(FALSE,FALSE,FALSE,FALSE),c(TRUE,FALSE,FALSE,FALSE)] Object of class "Mefa" ..@ xtab: 0 x 1 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left > x3[c(TRUE,FALSE,FALSE,FALSE),c(TRUE,FALSE,FALSE,FALSE)] Object of class "Mefa" ..@ xtab: 1 x 1 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left > > ## Melt > x0 <- Xtab(count ~ sample + species, x) > x33 <- Xtab(count ~ sample + species + segment, x) > (M1 <- Melt(x0)) rows cols value 1 Sample.1 Species.1 3 2 Sample.2 Species.1 10 3 Sample.2 Species.2 3 4 Sample.3 Species.3 4 > Melt(as.matrix(x0)) rows cols value 1 Sample.1 Species.1 3 2 Sample.2 Species.1 10 3 Sample.2 Species.2 3 4 Sample.3 Species.3 4 > (M2 <- Melt(x33)) rows cols segm value 1 Sample.1 Species.1 f 1 2 Sample.2 Species.1 f 10 3 Sample.3 Species.3 f 4 4 Sample.1 Species.1 m 2 5 Sample.2 Species.2 m 3 > stopifnot(identical(Xtab(value ~ rows + cols, M1), x0)) > stopifnot(identical(Xtab(value ~ rows + cols + segm, M2), x33)) > > ## stack > stack(x3) samp taxa values samp_samples samp_var1 taxa_specnames Sample.1 Sample.1 Species.1 3 Sample.1 1 Species.1 Sample.2 Sample.2 Species.1 10 Sample.2 2 Species.1 Sample.3 Sample.3 Species.1 0 Sample.3 1 Species.1 Sample.4 Sample.4 Species.1 0 Sample.4 2 Species.1 Sample.1.1 Sample.1 Species.2 0 Sample.1 1 Species.2 Sample.2.1 Sample.2 Species.2 3 Sample.2 2 Species.2 Sample.3.1 Sample.3 Species.2 0 Sample.3 1 Species.2 Sample.4.1 Sample.4 Species.2 0 Sample.4 2 Species.2 Sample.1.2 Sample.1 Species.3 0 Sample.1 1 Species.3 Sample.2.2 Sample.2 Species.3 0 Sample.2 2 Species.3 Sample.3.2 Sample.3 Species.3 4 Sample.3 1 Species.3 Sample.4.2 Sample.4 Species.3 0 Sample.4 2 Species.3 Sample.1.3 Sample.1 zero.pseudo 0 Sample.1 1 zero.pseudo Sample.2.3 Sample.2 zero.pseudo 0 Sample.2 2 zero.pseudo Sample.3.3 Sample.3 zero.pseudo 0 Sample.3 1 zero.pseudo Sample.4.3 Sample.4 zero.pseudo 0 Sample.4 2 zero.pseudo taxa_var2 Sample.1 b Sample.2 b Sample.3 b Sample.4 b Sample.1.1 a Sample.2.1 a Sample.3.1 a Sample.4.1 a Sample.1.2 b Sample.2.2 b Sample.3.2 b Sample.4.2 b Sample.1.3 a Sample.2.3 a Sample.3.3 a Sample.4.3 a > > ## accessing the xtab slot > xtab(x3) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 . . 4 . Sample.4 . . . . > ## replacing the slot value > x1[3,1] <- 999 > xtab(x3) <- x1 > xtab(x3) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 999 . 4 . Sample.4 . . . . > > ## accessing and replacing the samp slot > samp(x3) samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 Sample.4 2 > samp(x3) <- NULL > samp(x3) NULL > samp(x3) <- samp[1:3,] > samp(x3) samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 NA > > ## accessing and replacing the taxa slot > taxa(x3) specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo zero.pseudo a > taxa(x3) <- NULL > taxa(x3) NULL > taxa(x3) <- taxa[1:3,] > taxa(x3) specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo > > ## subsetting > unclass(x3[3:2, 1:2]) attr(,"xtab") 2 x 2 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Sample.3 999 . Sample.2 10 3 attr(,"samp") samples var1 Sample.3 Sample.3 1 Sample.2 Sample.2 2 attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a attr(,"join") [1] "left" > unclass(x3[3:2,]) attr(,"xtab") 2 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.3 999 . 4 . Sample.2 10 3 . . attr(,"samp") samples var1 Sample.3 Sample.3 1 Sample.2 Sample.2 2 attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo attr(,"join") [1] "left" > unclass(x3[,1:2]) attr(,"xtab") 4 x 2 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Sample.1 3 . Sample.2 10 3 Sample.3 999 . Sample.4 . . attr(,"samp") samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 NA attr(,"taxa") specnames var2 Species.1 Species.1 b Species.2 Species.2 a attr(,"join") [1] "left" > > ## simple methods, dim, dimnames > dim(x5) [1] 3 4 > dimnames(x5) [[1]] [1] "Sample.1" "Sample.2" "Sample.3" [[2]] [1] "Species.1" "Species.2" "Species.3" "zero.pseudo" > dn <- list(paste("S", 1:dim(x5)[1], sep=""), + paste("SPP", 1:dim(x5)[2], sep="")) > dimnames(x5) <- dn > unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" SPP1 SPP2 SPP3 SPP4 S1 3 . . . S2 10 3 . . S3 . . 4 . attr(,"samp") samples var1 S1 Sample.1 1 S2 Sample.2 2 S3 Sample.3 1 attr(,"taxa") specnames var2 SPP1 Species.1 b SPP2 Species.2 a SPP3 Species.3 b SPP4 zero.pseudo a attr(,"join") [1] "inner" > dimnames(x5)[[1]] <- paste("S", 1:dim(x5)[1], sep="_") > unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" SPP1 SPP2 SPP3 SPP4 S_1 3 . . . S_2 10 3 . . S_3 . . 4 . attr(,"samp") samples var1 S_1 Sample.1 1 S_2 Sample.2 2 S_3 Sample.3 1 attr(,"taxa") specnames var2 SPP1 Species.1 b SPP2 Species.2 a SPP3 Species.3 b SPP4 zero.pseudo a attr(,"join") [1] "inner" > dimnames(x5)[[2]] <- paste("SPP", 1:dim(x5)[2], sep="_") > unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" SPP_1 SPP_2 SPP_3 SPP_4 S_1 3 . . . S_2 10 3 . . S_3 . . 4 . attr(,"samp") samples var1 S_1 Sample.1 1 S_2 Sample.2 2 S_3 Sample.3 1 attr(,"taxa") specnames var2 SPP_1 Species.1 b SPP_2 Species.2 a SPP_3 Species.3 b SPP_4 zero.pseudo a attr(,"join") [1] "inner" > > ## transpose > x5 Object of class "Mefa" ..@ xtab: 3 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: inner > t(x5) Object of class "Mefa" ..@ xtab: 4 x 3 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: inner > unclass(x5) attr(,"xtab") 3 x 4 sparse Matrix of class "dgCMatrix" SPP_1 SPP_2 SPP_3 SPP_4 S_1 3 . . . S_2 10 3 . . S_3 . . 4 . attr(,"samp") samples var1 S_1 Sample.1 1 S_2 Sample.2 2 S_3 Sample.3 1 attr(,"taxa") specnames var2 SPP_1 Species.1 b SPP_2 Species.2 a SPP_3 Species.3 b SPP_4 zero.pseudo a attr(,"join") [1] "inner" > unclass(t(x5)) attr(,"xtab") 4 x 3 sparse Matrix of class "dgCMatrix" S_1 S_2 S_3 SPP_1 3 10 . SPP_2 . 3 . SPP_3 . . 4 SPP_4 . . . attr(,"samp") specnames var2 SPP_1 Species.1 b SPP_2 Species.2 a SPP_3 Species.3 b SPP_4 zero.pseudo a attr(,"taxa") samples var1 S_1 Sample.1 1 S_2 Sample.2 2 S_3 Sample.3 1 attr(,"join") [1] "inner" > > ## aggregation, sums > groupSums(as.matrix(x2), 1, c(1,1,2)) Species.1 Species.2 Species.3 zero.pseudo 1 13 3 0 0 2 0 0 4 0 > groupSums(as.matrix(x2), 2, c(1,1,2,2)) 1 2 Sample.1 3 0 Sample.2 13 0 Sample.3 0 4 > groupSums(x2, 1, c(1,1,2)) 2 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo 1 13 3 . . 2 . . 4 . > groupSums(x2, 2, c(1,1,2,2)) 3 x 2 sparse Matrix of class "dgCMatrix" 1 2 Sample.1 3 . Sample.2 13 . Sample.3 . 4 > groupSums(x5, 1, c(1,1,2)) Object of class "Mefa" ..@ xtab: 2 x 4 sparse Matrix ..@ samp: NULL ..@ taxa: data frame with 2 variables ..@ join: inner > groupSums(x5, 2, c(1,1,2,2)) Object of class "Mefa" ..@ xtab: 3 x 2 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: NULL ..@ join: inner > ## aggregation, means > groupMeans(as.matrix(x2), 1, c(1,1,2)) Species.1 Species.2 Species.3 zero.pseudo 1 6.5 1.5 0 0 2 0.0 0.0 4 0 > groupMeans(as.matrix(x2), 2, c(1,1,2,2)) 1 2 Sample.1 1.5 0 Sample.2 6.5 0 Sample.3 0.0 2 > groupMeans(x2, 1, c(1,1,2)) 2 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo 1 6.5 1.5 . . 2 . . 4 . > groupMeans(x2, 2, c(1,1,2,2)) 3 x 2 sparse Matrix of class "dgCMatrix" 1 2 Sample.1 1.5 . Sample.2 6.5 . Sample.3 . 2 > groupMeans(x5, 1, c(1,1,2)) Object of class "Mefa" ..@ xtab: 2 x 4 sparse Matrix ..@ samp: NULL ..@ taxa: data frame with 2 variables ..@ join: inner > groupMeans(x5, 2, c(1,1,2,2)) Object of class "Mefa" ..@ xtab: 3 x 2 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: NULL ..@ join: inner > > ## back and foth -- this is important to keep in the vignette > as.stcs(x1) samp taxa count segm 1 Sample.1 Species.1 3 undefined 2 Sample.2 Species.1 10 undefined 3 Sample.2 Species.2 3 undefined 4 Sample.3 Species.1 999 undefined 5 Sample.3 Species.3 4 undefined 6 Sample.4 zero.pseudo 0 zero.pseudo > as.mefa(x1) An object of class 'mefa' containing $ xtab: 1019 individuals of 4 taxa in 4 samples, $ segm: 1 (all inclusive) segment, $ samp: table for samples not provided, $ taxa: table for taxa not provided. > as.stcs(x3) samp taxa count segm 1 Sample.1 Species.1 3 undefined 2 Sample.2 Species.1 10 undefined 3 Sample.2 Species.2 3 undefined 4 Sample.3 Species.1 999 undefined 5 Sample.3 Species.3 4 undefined 6 Sample.4 zero.pseudo 0 zero.pseudo > (a <- as.mefa(x3)) An object of class 'mefa' containing $ xtab: 1019 individuals of 4 taxa in 4 samples, $ segm: 1 (all inclusive) segment, $ samp: table for samples provided (2 variables), $ taxa: table for taxa provided (2 variables). > xtab(a) Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 0 0 0 Sample.2 10 3 0 0 Sample.3 999 0 4 0 Sample.4 0 0 0 0 > samp(a) samples var1 Sample.1 Sample.1 1 Sample.2 Sample.2 2 Sample.3 Sample.3 1 Sample.4 NA > taxa(a) specnames var2 Species.1 Species.1 b Species.2 Species.2 a Species.3 Species.3 b zero.pseudo > segm(a) Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 0 0 0 Sample.2 10 3 0 0 Sample.3 999 0 4 0 Sample.4 0 0 0 0 > segm(x3) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 999 . 4 . Sample.4 . . . . > as.Mefa(a) Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: data frame with 2 variables ..@ taxa: data frame with 2 variables ..@ join: left > as.Xtab(a) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 999 . 4 . Sample.4 . . . . > (s <- melt(a)) samp taxa count segm 1 Sample.1 Species.1 3 undefined 2 Sample.2 Species.1 10 undefined 3 Sample.2 Species.2 3 undefined 4 Sample.3 Species.1 999 undefined 5 Sample.3 Species.3 4 undefined 6 Sample.4 zero.pseudo 0 zero.pseudo > as.Xtab(s) 4 x 4 sparse Matrix of class "dgCMatrix" Species.1 Species.2 Species.3 zero.pseudo Sample.1 3 . . . Sample.2 10 3 . . Sample.3 999 . 4 . Sample.4 . . . . > as.Mefa(s) Object of class "Mefa" ..@ xtab: 4 x 4 sparse Matrix ..@ samp: NULL ..@ taxa: NULL ..@ join: left > melt(x1) samp taxa count segm 1 Sample.1 Species.1 3 undefined 2 Sample.2 Species.1 10 undefined 3 Sample.2 Species.2 3 undefined 4 Sample.3 Species.1 999 undefined 5 Sample.3 Species.3 4 undefined 6 Sample.4 zero.pseudo 0 zero.pseudo > melt(x3) samp taxa count segm 1 Sample.1 Species.1 3 undefined 2 Sample.2 Species.1 10 undefined 3 Sample.2 Species.2 3 undefined 4 Sample.3 Species.1 999 undefined 5 Sample.3 Species.3 4 undefined 6 Sample.4 zero.pseudo 0 zero.pseudo > ## sparse matrix list > as.mefa(x33) An object of class 'mefa' containing $ xtab: 35 individuals of 3 taxa in 4 samples, $ segm: 2 (non-nested) segments: f, m, $ samp: table for samples not provided, $ taxa: table for taxa not provided. > > ## mbind > x=matrix(1:4,2,2) > rownames(x) <- c("a","b") > colnames(x) <- c("A","B") > y=matrix(11:14,2,2) > rownames(y) <- c("b","c") > colnames(y) <- c("B","C") > > sampx <- data.frame(x1=1:2, x2=2:1, + stringsAsFactors = TRUE) > rownames(sampx) <- rownames(x) > sampy <- data.frame(x1=3:4, x3=10:11, + stringsAsFactors = TRUE) > rownames(sampy) <- rownames(y) > taxay <- data.frame(x1=1:2, x2=2:1, + stringsAsFactors = TRUE) > rownames(taxay) <- colnames(y) > taxax <- NULL > > mbind(x,y) A B C a 1 3 NA b 2 4 13 c NA 12 14 > mbind(as(x,"sparseMatrix"),as(y,"sparseMatrix")) 3 x 3 sparse Matrix of class "dgCMatrix" A B C a 1 3 NA b 2 4 13 c NA 12 14 > unclass(mbind(Mefa(x,sampx),Mefa(y,sampy,taxay))) attr(,"xtab") 3 x 3 sparse Matrix of class "dgCMatrix" A B C a 1 3 NA b 2 4 13 c NA 12 14 attr(,"samp") x1 x2 x3 a 1 2 NA b 2 1 10 c 4 NA 11 attr(,"taxa") x1 x2 A NA NA B 1 2 C 2 1 attr(,"join") [1] "left" > > ## 1x1 cases > unclass(Mefa(y,sampy,taxay)[1,1]) attr(,"xtab") 1 x 1 sparse Matrix of class "dgCMatrix" B b 11 attr(,"samp") x1 x3 b 3 10 attr(,"taxa") x1 x2 B 1 2 attr(,"join") [1] "left" > unclass(Mefa(y,sampy[,1,drop=FALSE],taxay[,1,drop=FALSE])) attr(,"xtab") 2 x 2 sparse Matrix of class "dgCMatrix" B C b 11 13 c 12 14 attr(,"samp") x1 b 3 c 4 attr(,"taxa") x1 B 1 C 2 attr(,"join") [1] "left" > unclass(Mefa(y,sampy[,1,drop=FALSE],taxay[,1,drop=FALSE])[1,1]) attr(,"xtab") 1 x 1 sparse Matrix of class "dgCMatrix" B b 11 attr(,"samp") x1 b 3 attr(,"taxa") x1 B 1 attr(,"join") [1] "left" > > z0 <- Mefa(y) > samp(z0) <- sampy[,1,drop=FALSE] > taxa(z0) <- taxay[,1,drop=FALSE] > unclass(z0) attr(,"xtab") 2 x 2 sparse Matrix of class "dgCMatrix" B C b 11 13 c 12 14 attr(,"samp") x1 b 3 c 4 attr(,"taxa") x1 B 1 C 2 attr(,"join") [1] "left" > xtab(z0) <- y[1,1,drop=FALSE] > unclass(z0) attr(,"xtab") 1 x 1 sparse Matrix of class "dgCMatrix" B b 11 attr(,"samp") x1 b 3 attr(,"taxa") x1 B 1 attr(,"join") [1] "left" > > > proc.time() user system elapsed 1.70 0.15 1.86