Paul Menzel
2011-Jul-31 21:41 UTC
[R] How to count numbers of a vector and use them as index values?
Dear R folks, I am sorry to ask this simple question, but my search for the right way/command was unsuccessful. I have a vector> x <- c(2, 2, 3, 3, 4, 6)Now the values of x should be considered the index of another vector with possible greater length, say 8, and the value should be how often the indexes appeared in the original vector x.> length(result)[1] 8> result[1] 0 2 2 1 0 1 0 0 Thank you in advance, Paul -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110731/347cca14/attachment.bin>
Sarah Goslee
2011-Jul-31 22:10 UTC
[R] How to count numbers of a vector and use them as index values?
Hi Paul, I would use something like this:> x <- c(2,2,3,3,4,6) > table(x)x 2 3 4 6 2 2 1 1> x <- factor(x, levels=1:8) > table(x)x 1 2 3 4 5 6 7 8 0 2 2 1 0 1 0 0 Sarah On Sun, Jul 31, 2011 at 5:41 PM, Paul Menzel <paulepanter at users.sourceforge.net> wrote:> Dear R folks, > > > I am sorry to ask this simple question, but my search for the right > way/command was unsuccessful. > > I have a vector > >> x <- c(2, 2, 3, 3, 4, 6) > > Now the values of x should be considered the index of another vector > with possible greater length, say 8, and the value should be how often > the indexes appeared in the original vector x. > >> length(result) > ?[1] 8 >> result > ?[1] 0 2 2 1 0 1 0 0 > > > Thank you in advance, > > Paul >-- Sarah Goslee http://www.functionaldiversity.org
Jeffrey Dick
2011-Jul-31 22:19 UTC
[R] How to count numbers of a vector and use them as index values?
Here's an attempt using sapply:> x <- c(2, 2, 3, 3, 4, 6) > ys <- 1:8 > sapply(ys, function(y) { length(which(x==y)) } )[1] 0 2 2 1 0 1 0 0 Jeff On Sun, Jul 31, 2011 at 2:41 PM, Paul Menzel <paulepanter at users.sourceforge.net> wrote:> Dear R folks, > > > I am sorry to ask this simple question, but my search for the right > way/command was unsuccessful. > > I have a vector > >> x <- c(2, 2, 3, 3, 4, 6) > > Now the values of x should be considered the index of another vector > with possible greater length, say 8, and the value should be how often > the indexes appeared in the original vector x. > >> length(result) > ?[1] 8 >> result > ?[1] 0 2 2 1 0 1 0 0 > > > Thank you in advance, > > Paul > > ______________________________________________ > 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. > >
"Dénes TÓTH"
2011-Jul-31 22:21 UTC
[R] How to count numbers of a vector and use them as index values?
See also ?tabulate. tabulate(x,8)> Hi Paul, > > I would use something like this: > >> x <- c(2,2,3,3,4,6) >> table(x) > x > 2 3 4 6 > 2 2 1 1 >> x <- factor(x, levels=1:8) >> table(x) > x > 1 2 3 4 5 6 7 8 > 0 2 2 1 0 1 0 0 > > Sarah > > On Sun, Jul 31, 2011 at 5:41 PM, Paul Menzel > <paulepanter at users.sourceforge.net> wrote: >> Dear R folks, >> >> >> I am sorry to ask this simple question, but my search for the right >> way/command was unsuccessful. >> >> I have a vector >> >>> x <- c(2, 2, 3, 3, 4, 6) >> >> Now the values of x should be considered the index of another vector >> with possible greater length, say 8, and the value should be how often >> the indexes appeared in the original vector x. >> >>> length(result) >> ?[1] 8 >>> result >> ?[1] 0 2 2 1 0 1 0 0 >> >> >> Thank you in advance, >> >> Paul >> > > > > -- > Sarah Goslee > http://www.functionaldiversity.org > > ______________________________________________ > 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. >
Paul Menzel
2011-Jul-31 22:23 UTC
[R] [solved] How to count numbers of a vector and use them as index values?
Dear Sarah, Am Sonntag, den 31.07.2011, 18:10 -0400 schrieb Sarah Goslee:> I would use something like this: > > > x <- c(2,2,3,3,4,6) > > table(x) > x > 2 3 4 6 > 2 2 1 1 > > x <- factor(x, levels=1:8) > > table(x) > x > 1 2 3 4 5 6 7 8 > 0 2 2 1 0 1 0 0awesome. Thank you. Looking further I found the article ?Thinking in R: vectors? on R-bloggers [1]. The given example there returned a list though by using `lapply()` and the author is asking at the end: ?Any R experts out there with suggestions for a non-lapply solution??. So Derek-Jones, Sarah just gave you the answer. A further note regarding the example in [1], instead of length(X[X == n]) to count the number of occurrences you can also use `sum(X == n)` relying on the fact that `TRUE` and `FALSE` are converted to the integers `1` and `0`. Thanks, Paul [1] http://www.r-bloggers.com/thinking-in-r-vectors/ -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110801/05d1aa33/attachment.bin>
Possibly Parallel Threads
- [Bug 69349] New: Random image corruptions (in Debian Wheezy with Linux 3.2)
- [PATCH] nouveau: fix acpi edid retrieval
- ?plot: Add an example on how to plot functions to the help of `plot`.
- How to search for R related topics in search engines?
- apply on function with vector as result