[R] counting the number of ones in a vector

David Reinke dreinke at dowlinginc.com
Fri Feb 26 19:43:56 CET 2010

The length will remain the same no matter what expression appears in the subscript. I suggest this:

sum(x == 1)

David Reinke

Senior Transportation Engineer/Economist
Dowling Associates, Inc.
180 Grand Avenue, Suite 250
Oakland, California 94612-3774
510.839.1742 x104 (voice)
510.839.0871 (fax)

 Please consider the environment before printing this e-mail.

Confidentiality Notice:  This e-mail message, including any attachments, is for the sole use of the intended recipient(s), and may contain confidential  and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Randall Wrong
Sent: Friday, February 26, 2010 6:44 AM
To: r-help at r-project.org
Subject: [R] counting the number of ones in a vector

 Dear R users,

I want to count the number of ones in a vector x.

That's what I did : length( x[x==1] )

Is that a good solution ?

Thank you very much,

	[[alternative HTML version deleted]]

R-help at r-project.org mailing list
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