Elaine Kuo
2013-Sep-20 23:14 UTC
[R] search species with all absence in a presence-absence matrix
Dear list I have a matrix composed of islandID as rows and speciesID as columns. IslandID: Island A, B, C….O (15 islands in total) SpeciesID: D0001, D0002, D0003….D0100 (100 species in total) The cell of the matrix describes presence (1) or absence (0) of the species in an island. Now I would like to search the species with absence (0) in all the islands (Island A to Island O.) Please kindly advise the R code for the search purpose. Thank you. Elaine [[alternative HTML version deleted]]
John Kane
2013-Sep-20 23:40 UTC
[R] search species with all absence in a presence-absence matrix
Once you learn to use dput() I am sure someone will be happy to help you. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example John Kane Kingston ON Canada> -----Original Message----- > From: elaine.kuo.tw at gmail.com > Sent: Sat, 21 Sep 2013 07:14:38 +0800 > To: r-help at r-project.org > Subject: [R] search species with all absence in a presence-absence matrix > > Dear list > > > > I have a matrix composed of islandID as rows and speciesID as columns. > > IslandID: Island A, B, C?.O (15 islands in total) > > SpeciesID: D0001, D0002, D0003?.D0100 (100 species in total) > > > > The cell of the matrix describes presence (1) or absence (0) of the > species > in an island. > > > > Now I would like to search the species with absence (0) > > in all the islands (Island A to Island O.) > > > > Please kindly advise the R code for the search purpose. > > Thank you. > > > > Elaine > > [[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.____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
arun
2013-Sep-21 00:57 UTC
[R] search species with all absence in a presence-absence matrix
Hi, Try this: ?set.seed(248) ?lst1<- lapply(1:1000,function(i) matrix( sample(0:1,15*100,replace=TRUE),ncol=100,dimnames=list(paste("Island",LETTERS[1:15]),? paste0("D",sprintf("%04d",1:100))))) ?lst2<-lst1[sapply(lst1,function(x) any(colSums(x)==0))] ##The above steps are just to create some matrices with zeros in all the "islands" mat1<-lst2[[1]] ?mat1[,colSums(mat1)==0,drop=FALSE] #???????? D0038 #Island A???? 0 #Island B???? 0 #Island C???? 0 #Island D???? 0 #Island E???? 0 #Island F???? 0 #Island G???? 0 #Island H???? 0 #Island I???? 0 #Island J???? 0 #Island K???? 0 #Island L???? 0 #Island M???? 0 #Island N???? 0 #Island O???? 0 colnames(mat1)[colSums(mat1)==0] #[1] "D0038" ?mat2<-lst2[[3]] ?colnames(mat2)[colSums(mat2)==0] #[1] "D0086" A.K. ----- Original Message ----- From: Elaine Kuo <elaine.kuo.tw at gmail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Friday, September 20, 2013 7:14 PM Subject: [R] search species with all absence in a presence-absence matrix Dear list I have a matrix composed of islandID as rows and speciesID as columns. IslandID: Island A, B, C?.O (15 islands in total) SpeciesID: D0001, D0002, D0003?.D0100 (100 species in total) The cell of the matrix describes presence (1) or absence (0) of the species in an island. Now I would like to search the species with absence (0) in all the islands (Island A to Island O.) Please kindly advise the R code for the search purpose. Thank you. Elaine ??? [[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.
arun
2013-Sep-21 15:51 UTC
[R] search species with all absence in a presence-absence matrix
Hi Elaine, #You can either convert the .xls dataset to .csv and read it with ?read.csv() or use one of the packages to read excel dataset library(XLConnect) wb<- loadWorkbook("is_matrix.xls") dataRM<- readWorksheet(wb,sheet="is_matrix",rownames=1) dim(dataRM) #[1] 22 23 ?mat1<- as.matrix(dataRM) matSub<- mat1[,colSums(mat1)==0,drop=FALSE] ?head(matSub,3) #???????? D0008 D0009 D0010 D0011 D0012 D3396 #Sakhalin???? 0???? 0???? 0???? 0???? 0???? 0 #Hokkaido???? 0???? 0???? 0???? 0???? 0???? 0 #Korea??????? 0???? 0???? 0???? 0???? 0???? 0 colnames(mat1)[colSums(mat1)==0] #[1] "D0008" "D0009" "D0010" "D0011" "D0012" "D3396" #I used lst1, lst2 etc just to create some data. A.K. ________________________________ From: Elaine Kuo <elaine.kuo.tw at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Saturday, September 21, 2013 7:10 AM Subject: Re: [R] search species with all absence in a presence-absence matrix Hello Arun, Thanks for the code. I replaced my file (dataRM) with lst2 but an error showed up. It said Error in colSums(mat1): x must be at an array or at least two dimensions I am sure the x (dataRM) is two-dimension. Please kindly advise any code modification. Also, I attach part of my data in the e-mail. Thank you again. Elaine Code load("f:/b_W_line/R_workspace/R_workspace_R_4874/R_workspace_RM/dataset_RM_3546.RData") ?dim(dataRM) ?str(dataRM) ?mat1<-dataRM[[1]] ?mat1[,colSums(mat1)==0,drop=FALSE](error) ?colnames(mat1)[colSums(mat1)==0] On Sat, Sep 21, 2013 at 8:57 AM, arun <smartpink111 at yahoo.com> wrote: Hi,>Try this: > >?set.seed(248) >?lst1<- lapply(1:1000,function(i) matrix( sample(0:1,15*100,replace=TRUE),ncol=100,dimnames=list(paste("Island",LETTERS[1:15]),? paste0("D",sprintf("%04d",1:100))))) >?lst2<-lst1[sapply(lst1,function(x) any(colSums(x)==0))] >##The above steps are just to create some matrices with zeros in all the "islands" >mat1<-lst2[[1]] >?mat1[,colSums(mat1)==0,drop=FALSE] >#???????? D0038 >#Island A???? 0 >#Island B???? 0 >#Island C???? 0 >#Island D???? 0 >#Island E???? 0 >#Island F???? 0 >#Island G???? 0 >#Island H???? 0 >#Island I???? 0 >#Island J???? 0 >#Island K???? 0 >#Island L???? 0 >#Island M???? 0 >#Island N???? 0 >#Island O???? 0 >colnames(mat1)[colSums(mat1)==0] >#[1] "D0038" >?mat2<-lst2[[3]] >?colnames(mat2)[colSums(mat2)==0] >#[1] "D0086" > >A.K. > > > > > >----- Original Message ----- >From: Elaine Kuo <elaine.kuo.tw at gmail.com> >To: "r-help at r-project.org" <r-help at r-project.org> >Cc: >Sent: Friday, September 20, 2013 7:14 PM >Subject: [R] search species with all absence in a presence-absence matrix > > >Dear list > > > >I have a matrix composed of islandID as rows and speciesID as columns. > >IslandID: Island A, B, C?.O (15 islands in total) > >SpeciesID: D0001, D0002, D0003?.D0100 (100 species in total) > > > >The cell of the matrix describes presence (1) or absence (0) of the species >in an island. > > > >Now I would like to search the species with absence (0) > >in all the islands (Island A to Island O.) > > > >Please kindly advise the R code for the search purpose. > >Thank you. > > > >Elaine > > >??? [[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. > >?????