I read in a data frame like this:
site elevation sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8 sp9 sp10 sp11 sp12 sp13 sp14 sp15 sp16 sp17 sp18
1 1.2 0 5 2 1 2 0 1 0 2 0 1 3 0 0 0 0 0 0
2 3.9 0 4 2 0 3 0 0 0 0 2 0 2 0 0 1 0 0 0
3 4.4 0 2 1 1 2 0 1 0 0 1 0 3 0 0 0 0 0 0
4 5.5 0 4 3 0 5 0 1 0 2 1 0 3 0 0 2 0 0 0
5 9.0 0 3 2 0 1 0 1 0 2 0 0 1 0 0 0 1 0 0
sp is species number
For each site, I want to count the number of species that have 2 or more
So want to get a list like [6,6,4,7,4] or an array with the siteID and those values
I've been stuffing around with nested for loops but there's gotta be a more elegant way to do it...
Not the same as your output, but...
> rowSums(dat[grepl("sp",colnames(dat))]>=2)
[1] 5 5 3 6 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With