Convert a presence/ absence matrix to a list of presence only

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Convert a presence/ absence matrix to a list of presence only

antu
this thread (http://r.789695.n4.nabble.com/how-to-combine-presence-only-data-sets-to-one-presence-absence-table-td830140.html ) covered the need for making present list list to a matrix,

Now, I ran into an opposite problem. I have a data matrix with 0, 1 on them, what I need now is list of presence only for each column name

ie, this is what I have

        spl_A spl_B spl_C
spcs1 1 1 0
spcs2 1 0 1
spcs3 0 1 1


this is what I need

spl_A  spl_B spl_C
spcs1  spcs1 spcs2
spcs2 spcs3 spcs3



Thank you


Ananta Acharya
Graduate Student
Reply | Threaded
Open this post in threaded view
|

Re: Convert a presence/ absence matrix to a list of presence only

jholtman
try this; added an extra row so that the lengths would not be even:

> x <- read.table(textConnection("       spl_A   spl_B   spl_C
+ spcs1   1       1       0
+ spcs2   1       0       1
+ spcs3   0       1       1
+ spcs4   0       0       1"), header = TRUE)
> closeAllConnections()
> x
      spl_A spl_B spl_C
spcs1     1     1     0
spcs2     1     0     1
spcs3     0     1     1
spcs4     0     0     1
>
> # get maximum in any column so we can pad out the lengths
> maxLen <- max(colSums(x))
>
>
> combine <- lapply(x, function(.col){
+     sp <- row.names(x)[.col == 1]
+     c(sp, rep("", maxLen - length(sp)))
+ })
>
> do.call(cbind, combine)
     spl_A   spl_B   spl_C
[1,] "spcs1" "spcs1" "spcs2"
[2,] "spcs2" "spcs3" "spcs3"
[3,] ""      ""      "spcs4"
>


On Fri, Apr 22, 2011 at 2:10 PM, antu <[hidden email]> wrote:

> this thread
> (http://r.789695.n4.nabble.com/how-to-combine-presence-only-data-sets-to-one-presence-absence-table-td830140.html
> ) covered the need for making present list list to a matrix,
>
> Now, I ran into an opposite problem. I have a data matrix with 0, 1 on them,
> what I need now is list of presence only for each column name
>
> ie, this is what I have
>
>        spl_A   spl_B   spl_C
> spcs1   1       1       0
> spcs2   1       0       1
> spcs3   0       1       1
>
>
> this is what I need
>
> spl_A     spl_B spl_C
> spcs1  spcs1    spcs2
> spcs2    spcs3  spcs3
>
>
>
> Thank you
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Convert-a-presence-absence-matrix-to-a-list-of-presence-only-tp3468479p3468479.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Convert a presence/ absence matrix to a list of presence only

antu
Thank you, it perfectly worked, except I had to modify some codes on the rep("", maxLen - length(sp)) because it gave me some error, thanks



Ananta Acharya
Graduate Student