There's almost always a better way than a loop (although
sometimes it isn't worth the effort to figure it out). This time
it's straightforward:
> apply(c5[, 2:4], 1, function(x)sum(x > 0))
[1] 3 2 1 0 3
Sarah
On Tue, Feb 21, 2012 at 6:04 PM, Valerie Moore <vmoore2020 at yahoo.com>
wrote:> Hi,
>
> Is there a short way of doing this?
>
> I have the following table in R:
>
> 12.0 ? ? ?? 0.5 ? ? ? 0.6 ? ?? 0.2????? 0????? 0
>
> 12.3??????? 1.2?????? 0.8?????? 0??????? 0? ? ? 0
>
> 13.1???????? 0???????? 1.2 ? ? ? 0??????? 0 ? ?? 0
>
> 10.1 ? ? ? ? 0??????????? 0?????? 0?????? 1.3???? 0
>
> 10.2??????? 1.3??????? 1.2???? 0.9????? 0???? 1.3
>
>
> So I have 5 rows and 6 columns. For each row, I want to count the number
of? times that the value is greater than 1 (FOR COLUMNS 2 TO 4).
> I have the following R code.
>
> count=numeric()
> for (i in 1:5) {
> count[i]=sum(c5[i,2:4]>0)
> }
>
> The result is
>> count
> ? [1] 3 2 1 0 3
>
> Is there a short way of doing this (instead of doing the loop)?
>
> Thank you.
>
>
> -Valerie
--
Sarah Goslee
http://www.functionaldiversity.org