[R] Subsetting a square marix
David L Carlson
dcarlson at tamu.edu
Tue Jan 5 19:23:08 CET 2016
Assuming I've reconstructed your data correctly:
> dta
Sp1 Sp2 Sp3 Sp4 Sp5 Sp6
Sp1 0 1 0 0 0 0
Sp2 0 0 0 1 0 0
Sp3 1 0 0 0 1 0
Sp4 0 0 1 1 0 1
Sp5 0 1 0 0 0 1
Sp6 0 0 0 0 0 0
> dput(dta)
structure(list(Sp1 = c(0L, 0L, 1L, 0L, 0L, 0L), Sp2 = c(1L, 0L,
0L, 0L, 1L, 0L), Sp3 = c(0L, 0L, 0L, 1L, 0L, 0L), Sp4 = c(0L,
1L, 0L, 1L, 0L, 0L), Sp5 = c(0L, 0L, 1L, 0L, 0L, 0L), Sp6 = c(0L,
0L, 0L, 1L, 1L, 0L)), .Names = c("Sp1", "Sp2", "Sp3", "Sp4",
"Sp5", "Sp6"), class = "data.frame", row.names = c("Sp1", "Sp2",
"Sp3", "Sp4", "Sp5", "Sp6"))
The results of dput(dta) is what you should include in your plain text email.
As for the subset, your email indicated: sp1, sp3 and spp. But none of these are labels in your data set since R is case sensitive. Try for example:
> sub <- c("Sp1", "Sp3", "Sp5")
> dta[sub, sub]
Sp1 Sp3 Sp5
Sp1 0 0 0
Sp3 1 0 1
Sp5 0 0 0
And definitely spend some time with the available free R tutorials so that you understand how R works.
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Sarah Goslee
Sent: Tuesday, January 5, 2016 10:15 AM
To: Tawanda Tarakini
Cc: r-help
Subject: Re: [R] Subsetting a square marix
It really isn't clear what you want, and posting in HTML has mangled
what you did provide.
Please use dput() to provide sample data, and give us a clear idea of
what you want, ideally an example of what the output should look like.
Adding the R code you've tried to use is also a good idea.
Sarah
On Tue, Jan 5, 2016 at 4:06 AM, Tawanda Tarakini
<tawandatizora at gmail.com> wrote:
> I have a global matrix (e.g. table below) of species feeding. I am trying
> to create specific matrix for specific sites. If for example a subset is to
> have sp1, sp3 and spp only these 3 species should be appearing in the
> subset (both column and rows).
>
> I have been checking online help but I seem not to get my scenario
>
>
>
> Sp1
>
> Sp2
>
> Sp3
>
> Sp4
>
> Sp5
>
> Sp6
>
> Sp1
>
> 0
>
> 0
>
> 1
>
> 0
>
> 0
>
> 0
>
> Sp2
>
> 1
>
> 0
>
> 0
>
> 0
>
> 1
>
> 0
>
> Sp3
>
> 0
>
> 0
>
> 0
>
> 1
>
> 0
>
> 0
>
> Sp4
>
> 0
>
> 1
>
> 0
>
> 1
>
> 0
>
> 0
>
> Sp5
>
> 0
>
> 0
>
> 1
>
> 0
>
> 0
>
> 0
>
> Sp6
>
> 0
>
> 0
>
> 0
>
> 1
>
> 1
>
> 0
>
> --
> Kind Regards
>
> Tawanda Tarakini
>
> Lecturer and Industrial attachment coordinator
> Department of Wildlife, Ecology and Conservation
> Chinhoyi University of Technology
> Bag 7724, Chinhoyi
> Cell: +263 775 321 722
> Alternative email: ttarakini at cut.ac.zw
>
> [[alternative HTML version deleted]]
>
--
Sarah Goslee
http://www.numberwright.com
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
More information about the R-help
mailing list