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, Randall [[alternative HTML version deleted]]
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, Randall [[alternative HTML version deleted]]
What you did works well. You could also try the following. table(x)["1"] -----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 9:41 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, Randall [[alternative HTML version deleted]] ______________________________________________ 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. ================================== P Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S.News & World Report (2009). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use\...{{dropped:13}}
Try: sum(x == 1) On Fri, Feb 26, 2010 at 11:40 AM, Randall Wrong <randall.wrong at gmail.com> wrote:> 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, > Randall > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Hi Randall, Try also sum( x==1 ) HTH, Jorge On Fri, Feb 26, 2010 at 9:40 AM, Randall Wrong <> wrote:> 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, > Randall > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi Randall, Try also sum( x==1 ) HTH, Jorge On Fri, Feb 26, 2010 at 9:40 AM, Randall Wrong <> wrote:> 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, > Randall > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
I got tired of writing length(which()) so I define a useful function which I source in my .Rprofile: count <- function( x ) length(which(x)) Then: count( x == 1 ) -- View this message in context: http://n4.nabble.com/counting-the-number-of-ones-in-a-vector-tp1570700p1578549.html Sent from the R help mailing list archive at Nabble.com.
Nordlund, Dan (DSHS/RDA)
2010-Mar-04 19:09 UTC
[R] counting the number of ones in a vector
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On > Behalf Of sjaffe > Sent: Thursday, March 04, 2010 10:59 AM > To: r-help at r-project.org > Subject: Re: [R] counting the number of ones in a vector > > > I got tired of writing length(which()) so I define a useful function which I > source in my .Rprofile: > > count <- function( x ) length(which(x)) > > Then: > > count( x == 1 ) >How about sum(x==1) ? No need to write a new function, and it is even 2 characters less to type. Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204