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-11 2024-05-20 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.7675734 1.0757172 0.3224464 -1.1876703 -0.06331708 [2,] -1.3149660 1.6878613 -1.3125798 0.8624908 0.71582968 [3,] -0.6496280 -1.7716588 1.6546345 -1.4754642 1.44849807 [4,] 1.6944041 -1.7453999 1.0479371 -0.3364590 -0.14261859 [5,] -0.1949423 0.1817053 0.2277005 0.4735492 -1.56990212 [6,] -0.5370476 1.6093259 -0.8548440 -0.6356644 0.37341731 [7,] -0.1109475 -0.6103099 -0.3726816 0.9724810 -1.25048293 [8,] 0.5708555 -0.6809409 -1.0487677 -1.9608631 0.45959244 [9,] -0.3557743 3.1083411 1.7966682 2.5006516 -0.50481347 [10,] 0.4336537 -0.5740994 1.2328612 0.3101337 0.94254641 fnd_mx> (m <- find_max(mat)) index value 1 X2 1.0757172 2 X2 1.6878613 3 X3 1.6546345 4 X1 1.6944041 5 X4 0.4735492 6 X2 1.6093259 7 X4 0.9724810 8 X1 0.5708555 9 X2 3.1083411 10 X3 1.2328612 fnd_mx> ## column indices fnd_mx> as.integer(m$index) [1] 2 2 3 1 4 2 4 1 2 3 fnd_mx> find_min(mat) index value 1 X4 -1.1876703 2 X1 -1.3149660 3 X2 -1.7716588 4 X2 -1.7453999 5 X5 -1.5699021 6 X3 -0.8548440 7 X5 -1.2504829 8 X4 -1.9608631 9 X5 -0.5048135 10 X2 -0.5740994 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 10 0 0 0 0 b 0 14 0 0 0 c 0 19 0 0 0 d 0 0 15 0 0 e 0 0 0 25 0 f 0 0 0 16 0 0 0 0 0 1 fnd_mx> table(x, reclass(x, map, all = TRUE), useNA="always") x A B C D E a 10 0 0 0 0 0 b 0 14 0 0 0 0 c 0 19 0 0 0 0 d 0 0 15 0 0 0 e 0 0 0 25 0 0 f 0 0 0 16 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 10 0 0 0 b 0 14 0 0 c 0 19 0 0 d 0 0 0 15 e 0 0 25 0 f 0 0 16 0 0 0 0 1 fnd_mx> table(x, reclass(x, map, all = TRUE, allow_NA = TRUE), useNA="always") x A B D a 10 0 0 0 b 0 14 0 0 c 0 19 0 0 d 0 0 0 15 e 0 0 25 0 f 0 0 16 0 0 0 0 1 fnd_mx> (mat2 <- exp(mat) / rowSums(exp(mat))) [,1] [,2] [,3] [,4] [,5] [1,] 0.27942087 0.38026295 0.17903694 0.03954643 0.12173282 [2,] 0.02591427 0.52197572 0.02597618 0.22866328 0.19747054 [3,] 0.05017270 0.01633713 0.50256936 0.02196906 0.40895175 [4,] 0.54157169 0.01736873 0.28372585 0.07106624 0.08626749 [5,] 0.16161562 0.23553707 0.24662366 0.31535918 0.04086447 [6,] 0.07313630 0.62559197 0.05322501 0.06626806 0.18177865 [7,] 0.17694752 0.10739254 0.13619927 0.52284320 0.05661748 [8,] 0.40680348 0.11634195 0.08053622 0.03234992 0.36396842 [9,] 0.01671827 0.53411838 0.14387496 0.29088501 0.01440338 [10,] 0.16297081 0.05949058 0.36241086 0.14403425 0.27109350 fnd_mx> (rmat2 <- redistribute(mat2, source = 1, target = 2:4)) [,1] [,2] [,3] [,4] [,5] [1,] 0 0.55769311 0.26257532 0.05799874 0.12173282 [2,] 0 0.53939313 0.02684296 0.23629337 0.19747054 [3,] 0 0.01785259 0.54918870 0.02400695 0.40895175 [4,] 0 0.04264386 0.69660620 0.17448245 0.08626749 [5,] 0 0.28326813 0.29660139 0.37926601 0.04086447 [6,] 0 0.68699904 0.05844949 0.07277282 0.18177865 [7,] 0 0.13218635 0.16764371 0.64355247 0.05661748 [8,] 0 0.32281016 0.22346117 0.08976025 0.36396842 [9,] 0 0.54333475 0.14635757 0.29590431 0.01440338 [10,] 0 0.07662190 0.46677324 0.18551136 0.27109350 fnd_mx> colMeans(mat2) [1] 0.1895272 0.2614417 0.2014178 0.1732985 0.1743149 fnd_mx> colMeans(rmat2) [1] 0.0000000 0.3202803 0.2894500 0.2159549 0.1743149 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 C 13.17137 26 A 10.57621 24 D 11.62964 23 B 14.14033 27 ---------- 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.79 0.23 2.01