Re: [R] counting the number of ones in a vector

2010-03-09 Thread Randall Wrong
Thank you Gavin and Bert 2010/3/4 Gavin Simpson gavin.simp...@ucl.ac.uk On Thu, 2010-03-04 at 00:03 +0100, Randall Wrong wrote: Thanks to all of you ! (Benjamin Nutter, Henrique Dallazuanna, Tobias Verbeke, Jorge Ivan Velez, David Reinke and Gavin Simpson) x - c(1, 1, 1, NA, NA,

Re: [R] counting the number of ones in a vector

2010-03-04 Thread Gavin Simpson
On Thu, 2010-03-04 at 00:03 +0100, Randall Wrong wrote: Thanks to all of you ! (Benjamin Nutter, Henrique Dallazuanna, Tobias Verbeke, Jorge Ivan Velez, David Reinke and Gavin Simpson) x - c(1, 1, 1, NA, NA, 2, 1, NA) table(x)[1] 1 4 Why do I get two numbers ? It is a

Re: [R] counting the number of ones in a vector

2010-03-04 Thread sjaffe
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

Re: [R] counting the number of ones in a vector

2010-03-04 Thread Nordlund, Dan (DSHS/RDA)
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of sjaffe Sent: Thursday, March 04, 2010 10:59 AM To: r-help@r-project.org Subject: Re: [R] counting the number of ones in a vector I got tired of writing length(which()) so I

Re: [R] counting the number of ones in a vector

2010-03-03 Thread Randall Wrong
Thanks to all of you ! (Benjamin Nutter, Henrique Dallazuanna, Tobias Verbeke, Jorge Ivan Velez, David Reinke and Gavin Simpson) x - c(1, 1, 1, NA, NA, 2, 1, NA) table(x)[1] 1 4 Why do I get two numbers ? Thanks, Randall 2010/2/26 Nutter, Benjamin nutt...@ccf.org But if x has any

Re: [R] counting the number of ones in a vector

2010-03-03 Thread Bert Gunter
x - rep(c(A,B),c(4,3)) table(x)['A'] Capiche? Bert Gunter Genentech Nonclinical Biostatistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Randall Wrong Sent: Wednesday, March 03, 2010 3:03 PM To: Nutter, Benjamin; Henrique

Re: [R] counting the number of ones in a vector

2010-02-27 Thread Gavin Simpson
On Fri, 2010-02-26 at 10:43 -0800, David Reinke wrote: The length will remain the same no matter what expression appears in the subscript. No it won't! x == 1 evaluates to logical and when used to *subset* x, it *will* return the required answer. As observed with this example: set.seed(1) x

[R] counting the number of ones in a vector

2010-02-26 Thread Randall Wrong
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

Re: [R] counting the number of ones in a vector

2010-02-26 Thread Nutter, Benjamin
What you did works well. You could also try the following. table(x)[1] -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Randall Wrong Sent: Friday, February 26, 2010 9:41 AM To: r-help@r-project.org Subject: [R] counting the

[R] counting the number of ones in a vector

2010-02-26 Thread Randall Wrong
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

Re: [R] counting the number of ones in a vector

2010-02-26 Thread Henrique Dallazuanna
Try: sum(x == 1) On Fri, Feb 26, 2010 at 11:40 AM, Randall Wrong randall.wr...@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

Re: [R] counting the number of ones in a vector

2010-02-26 Thread Tobias Verbeke
Nutter, Benjamin wrote: What you did works well. You could also try the following. table(x)[1] or sum(x==1) HTH, Tobias -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Randall Wrong Sent: Friday, February 26, 2010 9:41 AM

Re: [R] counting the number of ones in a vector

2010-02-26 Thread Jorge Ivan Velez
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

Re: [R] counting the number of ones in a vector

2010-02-26 Thread Jorge Ivan Velez
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

Re: [R] counting the number of ones in a vector

2010-02-26 Thread Nutter, Benjamin
But if x has any missing values: x - c(1, 1, 1, NA, NA, 2, 1, NA) sum( x == 1) [1] NA sum(x==1, na.rm=TRUE) [1] 4 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Henrique Dallazuanna Sent: Friday, February 26, 2010 9:47

Re: [R] counting the number of ones in a vector

2010-02-26 Thread David Reinke
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)