[R] presence/absence matrix
I have a table such that: sample # species 1a 1b 2a 2c 3b and i would like to build a matrix with species names (i.e. a b and c) as field names and samples (i.e. 1,2 and 3) as row names and 1/0 as inner values such as: a b c 1 1 1 0 2 1 0 1 3 0 1 0 I am currently using Rcmdr package for managing datasets but need a function about I tried to use stack function but only with worst results Thanks Duccio __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] presence/absence matrix
See ?table i.e. x sample # species 11 a 21 b 32 a 42 c 53 b table(x) species sample # a b c 1 1 1 0 2 1 0 1 3 0 1 0 Regards, Francisco Francisco J. Zagmutt [EMAIL PROTECTED] wrote: I have a table such that: sample # species 1a 1b 2a 2c 3b and i would like to build a matrix with species names (i.e. a b and c) as field names and samples (i.e. 1,2 and 3) as row names and 1/0 as inner values such as: a b c 1 1 1 0 2 1 0 1 3 0 1 0 I am currently using Rcmdr package for managing datasets but need a function about I tried to use stack function but only with worst results Thanks Duccio __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] presence/absence matrix
What about table(sample,species) Stefano On Fri, Jul 20, 2007 at 04:16:39PM +0200, [EMAIL PROTECTED] wrote: rocchiniI have a table such that: rocchini rocchinisample # species rocchini1a rocchini1b rocchini2a rocchini2c rocchini3b rocchini rocchiniand i would like to build a matrix with species names (i.e. a b and c) rocchinias field names and samples (i.e. 1,2 and 3) as row names rocchiniand 1/0 as inner values rocchinisuch as: rocchinia b c rocchini1 1 1 0 rocchini2 1 0 1 rocchini3 0 1 0 rocchini rocchiniI am currently using Rcmdr package for managing datasets but need a rocchinifunction about I tried to use stack function but only with worst results rocchini rocchiniThanks rocchiniDuccio rocchini rocchini__ rocchiniR-help@stat.math.ethz.ch mailing list rocchinihttps://stat.ethz.ch/mailman/listinfo/r-help rocchiniPLEASE do read the posting guide http://www.R-project.org/posting-guide.html rocchiniand provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] presence/absence matrix
On 20-Jul-07 14:16:39, [EMAIL PROTECTED] wrote: I have a table such that: sample # species 1a 1b 2a 2c 3b and i would like to build a matrix with species names (i.e. a b and c) as field names and samples (i.e. 1,2 and 3) as row names and 1/0 as inner values such as: a b c 1 1 1 0 2 1 0 1 3 0 1 0 I am currently using Rcmdr package for managing datasets but need a function about I tried to use stack function but only with worst results The following generates the sort of thing you show above: ## Identities of possible species and samples Species-c(a,b,c,d) Samples-c(1,2,3,4,5,6) ## Generate a set of samples and corresponding species: species-sample(Species,20,replace=TRUE) samples=sample(Samples,20,replace=TRUE) ## Have a look: cbind(samples,species) samples species [1,] 5 c [2,] 2 c [3,] 4 a [4,] 5 b [5,] 5 d [6,] 1 d [7,] 2 b [8,] 2 b [9,] 3 b [10,] 2 d [11,] 4 d [12,] 1 b [13,] 3 b [14,] 2 c [15,] 2 d [16,] 6 b [17,] 5 a [18,] 4 a [19,] 2 b [20,] 5 a ## Now generate a table: T-table(samples,species) T species samples a b c d 1 0 1 0 1 2 0 3 2 2 3 0 2 0 0 4 2 0 0 1 5 2 1 1 1 6 0 1 0 0 Now this is a table structure, and also gives counts of occurrences and not merely presence/absence. So we need to get these out as a matrix. U-as.matrix(T) U species samples a b c d 1 0 1 0 1 2 0 3 2 2 3 0 2 0 0 4 2 0 0 1 5 2 1 1 1 6 0 1 0 0 U-1*(U0) U species samples a b c d 1 0 1 0 1 2 0 1 1 1 3 0 1 0 0 4 1 0 0 1 5 1 1 1 1 6 0 1 0 0 Is that what you want? Best wishes, Ted. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 094 0861 Date: 20-Jul-07 Time: 16:14:14 -- XFMail -- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.