[R] Count of entries in a row of a data frame
Phil Spector
spector at stat.berkeley.edu
Fri Mar 19 00:44:00 CET 2010
Thomas -
Two ways that come to mind are
apply(dat,1,function(x)sum(x != 0))
and
rowSums(dat!=0)
(assuming your data frame is named "dat").
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Thu, 18 Mar 2010, Thomas Jensen wrote:
> Dear R-list,
>
>
> I have data in the following format:
>
> Country 1 Country 2 Country 3 ...
> 1 0 1 0
> 2 1 0 2
> 3 0 1 0
>
> Now I would like to create a variable that counts every non-zero element
> of the rows, so for the above example it would be:
>
> Count
> 1 1
> 2 2
> 3 1
>
> Any help from is greatly appreciated!
>
> With kind regards
> Thomas
>
> ______________________________________________
> 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