Hello Arun

Thank for comments.

You are right. GID is the first column in the matrix this time.

In the second row of the first column, it used to be GID 1 in the first
e-mail.
But you are also right. You answered it already, and  this time In the
second row of the first column is 1.
Below is part of dput()(too many columns)

...... .Names = c("GID",
"D5291", "D5293", "D7414", "D7415", "D7416", "D7417", "D7418",
"D7419", "D7420", "D7421", "D7422", "D7423", "D7424", "D7425",
"D7426", "D7427", "D7428", "D7429", "D7430", "D7431", "D7432",
"D7433", "D7434", "D7435", "D7436", "D7437", "D7438", "D7439",
"D7440", "D7441", "D7442", "D7443", "D7444", "D7445", "D7446",
"
Elaine


On Thu, Aug 1, 2013 at 12:35 PM, arun <smartpink...@yahoo.com> wrote:

>
>
> Hi Elaine,
> I am not sure how your "original matrix" keeps on changing from the
> original post.  Here, your statement about rownames are 1, 2, 4 (the answer
> I already provided in the last post) and the matrix you showed looks
> different.  It seems like GID is the first column in the matrix.  I
> requested you to dput() the data to reduce these confusions.
>
> mat1<-as.matrix(read.table(text="
> GID       D0989   D9820  D5629  D4327  D2134
> 1               1        0        0       1      0
> 2               0        1        1       0      0
> 4               0        0        1       0      0
> 5               1        1        0       0      0
> 7               0        1        0       0      1
> ",sep="",header=TRUE))
> IslandA<-c("GID 1", "GID 5")
> IslandB<- c("GID 2", "GID 4", "GID
> 7")t(sapply(c("IslandA","IslandB"),function(x) {x1<-
> mat1[match(gsub(".*\\s+","",get(x)),mat1[,1]),-1];(!!colSums(x1))*1}))
> #        D0989 D9820 D5629 D4327 D2134
> #IslandA     1     1     0     1     0
> #IslandB     0     1     1     0     1
> A.K.
>
>
>
>
>
> ________________________________
> From: Elaine Kuo <elaine.kuo...@gmail.com>
> To: arun <smartpink...@yahoo.com>
> Cc: R help <r-help@r-project.org>
> Sent: Thursday, August 1, 2013 12:00 AM
> Subject: Re: [R] merge matrix row data
>
>
>
> Hello arun
>
> Thanks for the answers.
> I understand the answer to question 2.
> However, about question 1, sorry I did not clarify the question.
>
> 1. If the row names are 1, 2, and 4 etc (numbers) instead of GID 1, GID 2,
> and GID 3,
>    is there any modification in need for the code ?
>    The original data looks like below.
>
> Original matrix
> GID       D0989   D9820  D5629  D4327  D2134
> 1               1        0        0       1      0
> 2               0        1        1       0      0
> 4               0        0        1       0      0
> 5               1        1        0       0      0
> 7               0        1        0       0      1
>
> Resulting matrix
>                 D0989   D9820  D5629  D4327  D2134
> Island A   1        1       0       1       0
> Island B   0        1       1       0       1
>
> Elaine
>
>
>
> On Thu, Aug 1, 2013 at 7:15 AM, arun <smartpink...@yahoo.com> wrote:
>
> Hi Elaine,
> >
> >In that case:
> >Do you have "GID" in the "IslandA" and "IslandB"s?
> >
> >
> >IslandA<-c("GID 1", "GID 5")
> >IslandB<- c("GID 2", "GID 4", "GID 7")
> >
> >If there is no change in the two "Islands", then using the same dataset:
> >
> >
> >mat1<- as.matrix(read.table(text="
> >D0989  D9820  D5629  D4327  D2134
> >GID_1    1        0        0      1      0
> >GID_2    0        1        1      0      0
> >GID_4    0        0        1      0      0
> >GID_5    1        1        0      0      0
> >GID_7    0        1        0      0      1
> >",sep="",header=TRUE))
> >
> >row.names(mat1)<- gsub(".*\\_","",row.names(mat1)) #to replace the "GID_"
> from the row.names()
> > mat1
> >
> >#  D0989 D9820 D5629 D4327 D2134
> >
> >#1     1     0     0     1     0
> >#2     0     1     1     0     0
> >
> >#4     0     0     1     0     0
> >
> >#5     1     1     0     0     0
> >
> >#7     0     1     0     0     1
> >
> > IslandA<-c("GID 1", "GID 5")
> > IslandB<- c("GID 2", "GID 4", "GID 7")
> >res<-t(sapply(c("IslandA","IslandB"),function(x) {x1<-
> mat1[match(gsub(".*\\s+","",get(x)),row.names(mat1)),];(!!colSums(x1))*1}))
> >
> >res
> > #       D0989 D9820 D5629 D4327 D2134
> >#IslandA     1     1     0     1     0
> >#IslandB     0     1     1     0     1
> >
> >
> >Regarding the use of "!!colSums()"
> >You can check these:
> >
> > t(sapply(c("IslandA","IslandB"),function(x) {x1<-
> mat1[match(gsub(".*\\s+","",get(x)),row.names(mat1)),];!colSums(x1)}))
> >
> >#        D0989 D9820 D5629 D4327 D2134
> >#IslandA FALSE FALSE  TRUE FALSE  TRUE
> >#IslandB  TRUE FALSE FALSE  TRUE FALSE
> >
> >t(sapply(c("IslandA","IslandB"),function(x) {x1<-
> mat1[match(gsub(".*\\s+","",get(x)),row.names(mat1)),];!!colSums(x1)}))
> >
> >#        D0989 D9820 D5629 D4327 D2134
> >#IslandA  TRUE  TRUE FALSE  TRUE FALSE
> >#IslandB FALSE  TRUE  TRUE FALSE  TRUE
> >
> ># "*1" will replace TRUE with 1 and FALSE with 0.
> >
> >A.K.
> >
> >
> >
> >
> >
> >
> >
> >________________________________
> >From: Elaine Kuo <elaine.kuo...@gmail.com>
> >To: arun <smartpink...@yahoo.com>
> >Sent: Wednesday, July 31, 2013 6:58 PM
> >
> >Subject: Re: [R] merge matrix row data
> >
> >
> >
> >Dear Arun,
> >
> >Thank you for the clear explanation.
> >The row.names question is a mistyping, for I do not have enough sleep
> last night.
> >
> >Two more questions
> >
> >1. If the row names are 1, 2, and 4 etc (numbers) instead of GID 1, GID
> 2, and GID 3,
> >   is there any modification in need for the code ?
> >
> >2. Please kindly explain the code
> >    (!!colSums(x1))*1}
> >
> >   It is the critical part to merge the row data.
> >
> >Thanks again.
> >
> >Elaine
> >
> >
> >
> >On Thu, Aug 1, 2013 at 6:45 AM, arun <smartpink...@yahoo.com> wrote:
> >
> >Dear Elaine,
> >>
> >>I used that line only because you didn't provide the data using dput().
> So, I need to either use delimiter  "," or just leave a "space" by first
> joining the "GID" and the "numbers" using "_".  I chose the latter as I
> didn't had that much time to spent by putting "," between each entries.
> After that, I removed "_" using the ?gsub().  As Bert pointed out, there
> are many online resources for understanding regular expression.
> >>
> >>In this particular case, what I did was to single out the "_" in the
> first pair of quotes, and replace with  space in the second pair of quotes
> " ".  Therefore, "GID_1", would become "GID 1", which is what your original
> dataset looks like.
> >>
> >>If you type row.names(mat1) on the R console and enter, you will be able
> to get the output.
> >>
> >>Hope it helps.
> >>Arun
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>________________________________
> >>From: Elaine Kuo <elaine.kuo...@gmail.com>
> >>To: arun <smartpink...@yahoo.com>
> >>Cc: R help <r-help@r-project.org>
> >>Sent: Wednesday, July 31, 2013 5:07 PM
> >>Subject: Re: [R] merge matrix row data
> >>
> >>
> >>
> >>
> >>Dear Arun
> >>
> >>Thank you for the very useful help.
> >>However, please kindly explain the code below.
> >>row.names(mat1)<- gsub("[_]"," ",row.names(mat1))
> >>
> >>
> >>1. what does "[_]" mean?
> >>2. what does " "  mean?
> >>3. what does row.names(mat1) mean?
> >>
> >>I checked ?gsub but still did not get the idea.
> >>
> >>Thank you again
> >>
> >>Elaine
> >>
> >>
> >>
> >>On Wed, Jul 31, 2013 at 9:35 PM, arun <smartpink...@yahoo.com> wrote:
> >>
> >>HI,
> >>>
> >>>Please use ?dput()
> >>>mat1<- as.matrix(read.table(text="
> >>>
> >>>D0989  D9820  D5629  D4327  D2134
> >>>GID_1    1        0        0      1      0
> >>>GID_2    0        1        1      0      0
> >>>GID_4    0        0        1      0      0
> >>>GID_5    1        1        0      0      0
> >>>GID_7    0        1        0      0      1
> >>>",sep="",header=TRUE))
> >>>row.names(mat1)<- gsub("[_]"," ",row.names(mat1))
> >>>IslandA<-c("GID 1", "GID 5")
> >>>IslandB<- c("GID 2", "GID 4", "GID 7")
> >>> res<-  t(sapply(c("IslandA","IslandB"),function(x)
> {x1<-mat1[match(get(x),row.names(mat1)),];(!!colSums(x1))*1} ))
> >>>
> >>> res
> >>>#        D0989 D9820 D5629 D4327 D2134
> >>>#IslandA     1     1     0     1     0
> >>>#IslandB     0     1     1     0     1
> >>>A.K.
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>----- Original Message -----
> >>>From: Elaine Kuo <elaine.kuo...@gmail.com>
> >>>To: "r-h...@stat.math.ethz.ch" <r-h...@stat.math.ethz.ch>
> >>>Cc:
> >>>Sent: Wednesday, July 31, 2013 9:03 AM
> >>>Subject: [R] merge matrix row data
> >>>
> >>>Dear list,
> >>>
> >>>
> >>>
> >>>I have a matrix showing the species presence-absence on a map.
> >>>
> >>>Its rows are map locations, represented by GridCellID, such as GID1 and
> GID
> >>>5.
> >>>
> >>>Its columns are species ID, such as D0989, D9820, and D5629.
> >>>
> >>>The matrix is as followed.
> >>>
> >>>
> >>>
> >>>Now I want to merge the GridCellID according to the map location of each
> >>>island.
> >>>
> >>>For instance, Island A consist of GID 1 and 5. Island B consist of GID
> 2,
> >>>4, and 7.
> >>>
> >>>In GID 1 and 5, species D0989 are both 1.
> >>>
> >>>Then I want to merge GID 1 and 5 into Island A, with species D0989 as 1.
> >>>
> >>>The original matrix and the resulting matrix are listed below.
> >>>
> >>>Please kindly advise how to code the calculation in R.
> >>>
> >>>Please do not hesitate to ask if anything is unclear.
> >>>
> >>>Thank you in advance.
> >>>
> >>>
> >>>
> >>>Elaine
> >>>
> >>>
> >>>
> >>>Original matrix
> >>>
> >>>        D0989   D9820  D5629  D4327  D2134
> >>>
> >>>GID 1    1        0        0       1      0
> >>>
> >>>GID 2    0        1        1       0      0
> >>>
> >>>GID 4    0        0        1       0      0
> >>>
> >>>GID 5    1        1        0       0      0
> >>>
> >>>GID 7    0        1        0       0      1
> >>>
> >>>
> >>>
> >>>Resulting matrix
> >>>
> >>>                D0989   D9820  D5629  D4327  D2134
> >>>
> >>>Island A   1        1       0       1       0
> >>>
> >>>Island B   0        1       1       0       1
> >>>
> >>>    [[alternative HTML version deleted]]
> >>>
> >>>______________________________________________
> >>>R-help@r-project.org 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.
> >>>
> >>>
> >>
> >
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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.

Reply via email to