Dear R users, I'd like to count the number of integers in a vector y. Here is an example. y <- c(0,1,1,3,3,3,5,5,6) In fact, I know how to count the number of specific number in y. sum(y==0) -> 1 sum(y==1) -> 2 sum(y==2) -> 0 sum(y==3) -> 3 sum(y==4) -> 0 sum(y==5) -> 2 sum(y==6) -> 1 However, in one computation I want to get this vector [1,2,0,3,0,2,1]. Thank you in advance. Kathie -- View this message in context: http://r.789695.n4.nabble.com/Counting-the-number-of-integers-at-one-swoop-tp3901215p3901215.html Sent from the R help mailing list archive at Nabble.com.
Counting the number of integers at one swoop
6 messages · Kathie, Daniel Malter, R. Michael Weylandt +2 more
I think there must be an easier solution, but this works:
y <- c(0,1,1,3,3,3,5,5,6)
x<-matrix(0:6,ncol=1)
apply(x,1,function(x){length(y[y==x])})
HTH,
Daniel
Kathie wrote:
Dear R users, I'd like to count the number of integers in a vector y. Here is an example. y <- c(0,1,1,3,3,3,5,5,6) In fact, I know how to count the number of specific number in y. sum(y==0) -> 1 sum(y==1) -> 2 sum(y==2) -> 0 sum(y==3) -> 3 sum(y==4) -> 0 sum(y==5) -> 2 sum(y==6) -> 1 However, in one computation I want to get this vector [1,2,0,3,0,2,1]. Thank you in advance. Kathie
-- View this message in context: http://r.789695.n4.nabble.com/Counting-the-number-of-integers-at-one-swoop-tp3901215p3901356.html Sent from the R help mailing list archive at Nabble.com.
Table() or more generally tabulate() Though, as a general warning, you may need to be a little careful depending on the source of your data. Once you get into floating point business, the definition of an integer becomes a little less cut and dry. If your data are all integer, the data type, then there's nothing to worry about. Michael
On Thu, Oct 13, 2011 at 7:33 AM, Kathie <kathryn.lord2000 at gmail.com> wrote:
Dear R users, I'd like to count the number of integers in a vector y. Here is an example. y <- c(0,1,1,3,3,3,5,5,6) In fact, I know how to count the number of specific number in y. sum(y==0) -> 1 sum(y==1) -> 2 sum(y==2) -> 0 sum(y==3) -> 3 sum(y==4) -> 0 sum(y==5) -> 2 sum(y==6) -> 1 However, in one computation I want to get this vector [1,2,0,3,0,2,1]. Thank you in advance. Kathie -- View this message in context: http://r.789695.n4.nabble.com/Counting-the-number-of-integers-at-one-swoop-tp3901215p3901215.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
Slight addendum, tabulate() ignores zeros so you'll need to do tabulate(y+1). Table will handle zeros but won't look for values that never appear (in your example 2 & 4). Michael On Thu, Oct 13, 2011 at 8:51 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
Table() or more generally tabulate() Though, as a general warning, you may need to be a little careful depending on the source of your data. Once you get into floating point business, the definition of an integer becomes a little less cut and dry. If your data are all integer, the data type, then there's nothing to worry about. Michael On Thu, Oct 13, 2011 at 7:33 AM, Kathie <kathryn.lord2000 at gmail.com> wrote:
Dear R users, I'd like to count the number of integers in a vector y. Here is an example. y <- c(0,1,1,3,3,3,5,5,6) In fact, I know how to count the number of specific number in y. sum(y==0) -> 1 sum(y==1) -> 2 sum(y==2) -> 0 sum(y==3) -> 3 sum(y==4) -> 0 sum(y==5) -> 2 sum(y==6) -> 1 However, in one computation I want to get this vector [1,2,0,3,0,2,1]. Thank you in advance. Kathie -- View this message in context: http://r.789695.n4.nabble.com/Counting-the-number-of-integers-at-one-swoop-tp3901215p3901215.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
try this:
y <- c(0,1,1,3,3,3,5,5,6) x <- tabulate(y+1) names(x) <- seq(from = 0, by = 1, length = length(x)) x
0 1 2 3 4 5 6 1 2 0 3 0 2 1
On Thu, Oct 13, 2011 at 7:33 AM, Kathie <kathryn.lord2000 at gmail.com> wrote:
Dear R users, I'd like to count the number of integers in a vector y. Here is an example. y <- c(0,1,1,3,3,3,5,5,6) In fact, I know how to count the number of specific number in y. sum(y==0) -> 1 sum(y==1) -> 2 sum(y==2) -> 0 sum(y==3) -> 3 sum(y==4) -> 0 sum(y==5) -> 2 sum(y==6) -> 1 However, in one computation I want to get this vector [1,2,0,3,0,2,1]. Thank you in advance. Kathie -- View this message in context: http://r.789695.n4.nabble.com/Counting-the-number-of-integers-at-one-swoop-tp3901215p3901215.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111013/94b6ad37/attachment.pl>