[R] merge matrix row data
arun
smartpink111 at yahoo.com
Thu Aug 1 17:18:18 CEST 2013
HI Elaine,
From the error, it looks like there are cases where none of the elements in one of the element matches to the GID column of dataNP.m.
It's only a guess as you didn't provide information about the "Islands".
I was able to recreate the error you got.
dataNP.m<-structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), .Dim = c(20L,
14L), .Dimnames = list(NULL, c("GID", "D0407", "D0409", "D0410",
"D0462", "D0463", "D0473", "D0475", "D0488", "D0489", "D0492",
"D0493", "D0504", "D0536")))
Conti_Australia<- c(1,14,18)
Conti_Malay<- c(2,6,8)
Island_Sumatra<- c(3,9,21)
Island_New_Guinea<- c(22,24,28) #none of the elements are present in the GID column of example dataset
lapply(c("Conti_Australia","Conti_Malay","Island_Sumatra","Island_New_Guinea"),function(x) {x1<- match(get(x),dataNP.m[,1]); x2<-x1[!is.na(x1)];colSums(x2)})
#Error in colSums(x2) : 'x' must be an array of at least two dimensions
lst1<-sapply(c("Conti_Australia","Conti_Malay","Island_Sumatra","Island_New_Guinea"),function(x) {x1<- match(get(x),dataNP.m[,1]); x2<-x1[!is.na(x1)];if(length(x2)>0) (!!colSums(dataNP.m[x2,-1]))*1})
t(simplify2array(lst1[lapply(lst1,length)>0]))
# D0407 D0409 D0410 D0462 D0463 D0473 D0475 D0488 D0489 D0492
#Conti_Australia 0 0 0 0 0 1 0 0 0 0
#Conti_Malay 0 0 0 0 0 1 0 0 0 0
#Island_Sumatra 0 0 0 0 0 0 0 0 0 1
# D0493 D0504 D0536
#Conti_Australia 0 0 1
#Conti_Malay 1 0 1
#Island_Sumatra 0 0 0
A.K.
________________________________
From: Elaine Kuo <elaine.kuo.tw at gmail.com>
To: arun <smartpink111 at yahoo.com>
Sent: Thursday, August 1, 2013 9:20 AM
Subject: Re: [R] merge matrix row data
Hello arun,
It is Elaine again.
After running the function in the last sentence of your code,
an error message says
Error in colSums(x1) : 'x' must be an array of at least two dimensions
Please kindly help and thanks
Elaine
dput
> dput(head(dataNP.m,20))
structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), .Dim = c(20L,
14L), .Dimnames = list(NULL, c("GID", "D0407", "D0409", "D0410",
"D0462", "D0463", "D0473", "D0475", "D0488", "D0489", "D0492",
"D0493", "D0504", "D0536")))
Code
island<- t(sapply(c( "Conti_Australia","Conti_Korea","Conti_Malay",
+ "Island_Sumatra","Island_Bali","Island_Lombok",
+ "Island_Lesser_Sunda","Island_Maluku","Island_Sulawesi","Island_Kepu_Sula",
+ "Island_New_Guinea","Island_Palawan","Island_Phillipines",
+ "Island_Hainan","Island_Taiwan",
+ "Island_Sakishima","Island_Ryukyu","Island_Amami","Island_Osumi",
+ "Island_Kyushu","Island_Shikoku","Island_Honshu","Island_Hokkaido","Island_Sakhalin"),
+ function(x) {x1<- dataNP.m[match(get(x),dataNP.m[,1]),-1];(!!colSums(x1))*1}))
On Thu, Aug 1, 2013 at 8:41 PM, arun <smartpink111 at yahoo.com> wrote:
Hi Elaine,
>No problem.
>Regards,
>
>Arun
>
>
>
>
>
>________________________________
>From: Elaine Kuo <elaine.kuo.tw at gmail.com>
>To: arun <smartpink111 at yahoo.com>
>Sent: Thursday, August 1, 2013 8:40 AM
>
>Subject: Re: [R] merge matrix row data
>
>
>
>Yes, you are right. Now it is time to compare GID value and islands.
>It worked well. :P
>Thanks Arun very much.
>You made my work finish much earlier than I expected
>
>Have a nice day
>
>Elaine
>
>
>
>On Thu, Aug 1, 2013 at 8:34 PM, arun <smartpink111 at yahoo.com> wrote:
>
>Currently, I am really confused as to how your data looks like.
>>You showed me some data and I was able to process it. Now, what is with the row.names. We are not comparing the row.names anymore. Isn't the comparison between the GID column and the values in Islands?
>>Also, it is difficult to pinpoint just by looking at that piece of code. There are no errors or anything. It would be better if you tell me exactly what the problem is..
>>
>>
>>
>>
>>
>>
>>
>>________________________________
>>From: Elaine Kuo <elaine.kuo.tw at gmail.com>
>>To: arun <smartpink111 at yahoo.com>
>>Sent: Thursday, August 1, 2013 8:30 AM
>>
>>Subject: Re: [R] merge matrix row data
>>
>>
>>
>>Hello Arun,
>>
>>Thanks.
>>
>>However, the problem is that row.names cannot be read once it turns to be 1, 2, 3 instead of GID 1, GID 2, GID 3.
>>Therefore, it is not possible to copy value from GID to islands.
>>
>>Please kindly help indicate where the error could be. (response: NULL)
>>Thanks..
>>
>>Code
>> dataNP_1 <-read.dbf("H:/temp_D/stage_4_R_2748_S/NP_1067/Anseriformes_13.dbf", as.is = FALSE)
>>dataNP.m<- as.matrix(dataNP_1,header=TRUE)
>>row.names(dataNP.m)
>>
>>
>>
>>
>>On Thu, Aug 1, 2013 at 8:24 PM, arun <smartpink111 at yahoo.com> wrote:
>>
>>HI Elaine,
>>>
>>>So, according to your new post, it seems like:
>>>IslandA<- c("1","5")
>>>IslandB<- c("2","4","7")
>>>In that case, you don't need the gsub()
>>>
>>>
>>> t(sapply(c("IslandA","IslandB"),function(x) {x1<- mat1[match(get(x),mat1[,1]),-1];(!!colSums(x1))*1}))
>>># D0407 D0409 D0410 D0462 D0463 D0473 D0475 D0488 D0489 D0492 D0493 D0504
>>>
>>>#IslandA 0 0 0 0 0 0 0 0 0 0 0 0
>>>#IslandB 0 0 0 0 0 1 0 0 0 0 1 0
>>># D0536
>>>#IslandA 0
>>>#IslandB 1
>>>
>>>
>>>Also, Inline:
>>>
>>>
>>>
>>>
>>>________________________________
>>>From: Elaine Kuo <elaine.kuo.tw at gmail.com>
>>>To: arun <smartpink111 at yahoo.com>
>>>Sent: Thursday, August 1, 2013 8:17 AM
>>>
>>>Subject: Re: [R] merge matrix row data
>>>
>>>
>>>
>>>
>>>Let's solve the questions one by one.
>>>
>>>1. In the second row of the first column, it used to be GID 1 in the first e-mail. "
>>>Are you saying that first column is "GID", "GID", "GID", and 2nd column is 1, 2, 4 etc?
>>>
>>>=> No. The first column is GID, 1, 2, 3, .....But the first row of the first column is GID (column name.)
>>>
>>>
>>>2. This dput() is also not full (so can't be used). Trying subset a smaller dataset and dput that subset.
>>>=> No. This is a full set of data. I chose a genus with 13 species (Dxxxx).
>>>
>>>
>>>Let it be. if the output of dput() is not complete, it can be used. That is the reason I asked for dput(head(dataset,20)) so that it gets the first 20 rows of data.
>>>
>>>
>>>
>>> Please kindly forget the example in previous e-mails.
>>>
>>>3. Are they of the same format as below?
>>>
>>>
>>>
>>>IslandA<-c("GID 1", "GID 5")
>>>IslandB<- c("GID 2", "GID 4", "GID 7")
>>>
>>>=> Actually the GID range from 1 to 4873.
>>> IslandA and IslandB are example for actual islands.
>>> Please forget Island A and B and let's go into the real world.
>>> Here are some actual islands I just finished compiling.
>>>
>>> Island_Bali<-c("971","972")
>>> Island_Lombok<-c("973","946")
>>> Island_Kepu_Sula<-c("1163","1164")
>>>
>>>
>>>
>>>
>>>A.K.
>>>
>>>
>>>
>>>On Thu, Aug 1, 2013 at 8:01 PM, arun <smartpink111 at yahoo.com> wrote:
>>>
>>>Also, here the GID numbers are from 1 to 20. What is the corresponding GID's in IslandA and IslandB? Is it the same as you posted previously?
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>________________________________
>>>> From: Elaine Kuo <elaine.kuo.tw at gmail.com>
>>>>To: arun <smartpink111 at yahoo.com>
>>>>Sent: Thursday, August 1, 2013 7:57 AM
>>>>
>>>>Subject: Re: [R] merge matrix row data
>>>>
>>>>
>>>>
>>>>Thanks.
>>>>
>>>>
>>>>Here you go.
>>>>
>>>>
>>>>structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
>>>>13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L,
>>>>1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
>>>>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
>>>>1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), .Dim = c(20L,
>>>>14L), .Dimnames = list(NULL, c("GID", "D0407", "D0409", "D0410",
>>>>"D0462", "D0463", "D0473", "D0475", "D0488", "D0489", "D0492",
>>>>"D0493", "D0504", "D0536")))
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>On Thu, Aug 1, 2013 at 7:55 PM, arun <smartpink111 at yahoo.com> wrote:
>>>>
>>>>HI Elaine,
>>>>>It seems like you skippped a part of the dput() output. Without the full, it is not useful. Try copy and paste the output you showed in your R console. I am not asking for the full data to be dput(). Just
>>>>>dput(head(dataset,20))
>>>>>
>>>>>
>>>>>A.K.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>________________________________
>>>>>From: Elaine Kuo <elaine.kuo.tw at gmail.com>
>>>>>To: arun <smartpink111 at yahoo.com>
>>>>>Cc: R help <r-help at r-project.org>
>>>>>Sent: Thursday, August 1, 2013 5:25 AM
>>>>>
>>>>>Subject: Re: [R] merge matrix row data
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>Hello arun
>>>>>I modified your
>>>>>code a little bit but failed to retrieve the row.names.
>>>>>The response is
>>>>>NULL.
>>>>>Please kindly help
>>>>>and thanks.
>>>>>Elaine
>>>>>
>>>>>Code
>>>>>load("h:/b_W_line/R_workspace/dataset_Residence_2748.RData")
>>>>> dim(dataR)
>>>>> str(dataR)
>>>>> dataR.m<- as.matrix(dataR, header=TRUE)
>>>>> dataR.m[,1]
>>>>> row.names(dataR.m)
>>>>>
>>>>>dput(dataR.m)
>>>>>.skipped...0L,
>>>>>0L, 0L, 0L,
>>>>>0L, 0L, 0L,
>>>>>0L, 0L, 0L,
>>>>>0L, 0L,
>>>>>0L, 0L, 0L),
>>>>>.Dim = c(4873L, 14L), .Dimnames = list(NULL,
>>>>> c("GID", "D0407",
>>>>>"D0409", "D0410", "D0462", "D0463",
>>>>>"D0473",
>>>>> "D0475",
>>>>>"D0488", "D0489", "D0492", "D0493",
>>>>>"D0504", "D0536"
>>>>>
>>>>>)))
>>>>>
>>>>>
>>>>>
>>>>>On Thu, Aug 1, 2013 at 1:24 PM, Elaine Kuo <elaine.kuo.tw at gmail.com> wrote:
>>>>>
>>>>>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 <smartpink111 at 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.tw at gmail.com>
>>>>>>>To: arun <smartpink111 at yahoo.com>
>>>>>>>Cc: R help <r-help at 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 <smartpink111 at 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.tw at gmail.com>
>>>>>>>>To: arun <smartpink111 at 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 <smartpink111 at 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.tw at gmail.com>
>>>>>>>>>To: arun <smartpink111 at yahoo.com>
>>>>>>>>>Cc: R help <r-help at 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 <smartpink111 at 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.tw at gmail.com>
>>>>>>>>>>To: "r-help at stat.math.ethz.ch" <r-help at 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 at 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.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>
>>
>
More information about the R-help
mailing list