[R] search species with all absence in a presence-absence matrix

Richard Kwock richardkwock at gmail.com
Sat Sep 21 01:57:46 CEST 2013


Hi,

I believe the function you are looking for is:

which("yourdata" == 0, arr.ind = T)

The "arr.ind" parameter in the "which" function will return you a
matrix with row, column indices for where there are 0's in your
dataset.

set.seed(6584)
data <- matrix(sample(c(0,1), 36, replace = T), nc = 6, dimnames =
list(c(paste("c",1:6, sep = "")), paste("r", 1:6, sep = "")))
data
 #  r1 r2 r3 r4 r5 r6
#c1  1  1  0  1  0  1
#c2  1  0  0  0  1  0
#c3  1  0  1  0  0  1
#c4  1  1  0  0  0  1
#c5  0  0  0  1  0  1
#c6  0  0  1  1  1  0

array_indices <- which(data == 0, arr.ind = T)
array_indices

#   row col
#c5   5   1
#c6   6   1
#c2   2   2
#c3   3   2
#c5   5   2
#c6   6   2
#c1   1   3
#c2   2   3
#c4   4   3
#c5   5   3
#c2   2   4
#c3   3   4
#c4   4   4
#c1   1   5
#c3   3   5
#c4   4   5
#c5   5   5
#c2   2   6
#c6   6   6

cbind(row = rownames(data)[array_indices[,1]], col =
colnames(data)[array_indices[,2]])

The last command will get you rownames and colnames from your dataset.

Richard

On Fri, Sep 20, 2013 at 4:40 PM, John Kane <jrkrideau at inbox.com> wrote:
> 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!
>
> ______________________________________________
> 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