[R-sig-eco] removing singleton taxa

Scott Foster scott.foster at csiro.au
Tue Dec 18 01:36:34 CET 2012


Hi Kate,

This is pretty terse, but try

df2 <- df1[,apply( df1, 2, function(x) sum( x>0)) > 1]

This subsets the columns of df1 to those that have more than 1 presence.

You may want to have a look at ?apply to see how it works.

Good luck,

Scott

PS  There are many ways to skin this cat.  For example, you could look 
at colSums( df1>0), for example.  This would do exactly the same thing 
as the apply() in the above.

On 18/12/12 11:22, Kate Boersma wrote:
> Hi list. I am new to R and stuck on a very simple problem. Forgive me if
> this is not the right forum for my question - feel free to refer me
> elsewhere.
>
> I have a community matrix of sites*species, and I want to remove species
> that only occur in a single site (singleton taxa). It sounds so simple,
> but I have spent hours (no joke) with google and my reference books and
> am still stuck.
>
> Here's some example code:
> Species1 = sample(0:20, 10, replace=TRUE)
> Species2 = sample(0:20, 10, replace=TRUE)
> Species3 = c(0,0,0,0,0,0,0,4,0,0)
> df1 = data.frame(Species1, Species2, Species3)
> df1
>
> I want to remove Species 3. Basically, I want R to count the number of
> non-zero cells in a column and remove the column if that number is 1 or 0.
>
> I have tried messing around with rowsums(), length(), table() and
> specnumber() in the vegan package and can't figure it out.
>
> Thanks,
> Kate
>

-- 
Scott Foster
CSIRO Mathematics, Informatics and Statistics
GPO Box 1538
Castray Esplanade
Hobart 7001
Tasmania
Australia

Phone:     (03) 6232 5178
Fax:       (03) 6232 5000
Email:     scott.foster at csiro.au



More information about the R-sig-ecology mailing list