Hi all, I've a vector with entries, which are all of the same type, e.g. string: k <- c("bb", "bb", "bb", "aa", "cc", "cc") and want to create a second vector containing the number of each entry in k in the same order as in k, i.e. c(3, 1, 2) or: k <- c(5,5,5,5,2,2,4) => c(4,2,1) thanks
axionator <axionator <at> gmail.com> writes:> I've a vector with entries, which are all of the same type, e.g. string: > k <- c("bb", "bb", "bb", "aa", "cc", "cc") > and want to create a second vector containing the number of each entry > in k in the same order as in k, i.e. > c(3, 1, 2)table(k) Ben Bolker
Take a look at the run-length encoding function rle. I believe rle(k)$lengths gives you exactly what you want. -s On Wed, Feb 4, 2009 at 10:19 AM, axionator <axionator at gmail.com> wrote:> Hi all, > I've a vector with entries, which are all of the same type, e.g. string: > k <- c("bb", "bb", "bb", "aa", "cc", "cc") > and want to create a second vector containing the number of each entry > in k in the same order as in k, i.e. > c(3, 1, 2) > > or: > k <- c(5,5,5,5,2,2,4) > => c(4,2,1) > > thanks > > ______________________________________________ > 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: table(k)[rank(unique(k))] -ian Armin Meier wrote:> > Hi all, > I've a vector with entries, which are all of the same type, e.g. string: > k <- c("bb", "bb", "bb", "aa", "cc", "cc") > and want to create a second vector containing the number of each entry > in k in the same order as in k, i.e. > c(3, 1, 2) > > or: > k <- c(5,5,5,5,2,2,4) > => c(4,2,1) > > thanks > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/counting-entries-in-vector-tp21832564p21833066.html Sent from the R help mailing list archive at Nabble.com.
Try: table(k) On Wed, Feb 4, 2009 at 1:19 PM, axionator <axionator@gmail.com> wrote:> Hi all, > I've a vector with entries, which are all of the same type, e.g. string: > k <- c("bb", "bb", "bb", "aa", "cc", "cc") > and want to create a second vector containing the number of each entry > in k in the same order as in k, i.e. > c(3, 1, 2) > > or: > k <- c(5,5,5,5,2,2,4) > => c(4,2,1) > > thanks > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
try this: k <- c("bb", "bb", "bb", "aa", "cc", "cc") f <- factor(k, levels = unique(k)) as.vector(table(f)) you can put it in one line but it's less readable. I hope it helps. Best, Dimitris axionator wrote:> Hi all, > I've a vector with entries, which are all of the same type, e.g. string: > k <- c("bb", "bb", "bb", "aa", "cc", "cc") > and want to create a second vector containing the number of each entry > in k in the same order as in k, i.e. > c(3, 1, 2) > > or: > k <- c(5,5,5,5,2,2,4) > => c(4,2,1) > > thanks > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Its not clear whether c("bb", "bb", "aa", "aa", "bb") can occur or if it can how it should be handled but this gives the lengths of each run and so would give c(2, 2, 1) in that case (as opposed to c(3, 2)): rle(k)$lengths On Wed, Feb 4, 2009 at 10:19 AM, axionator <axionator at gmail.com> wrote:> Hi all, > I've a vector with entries, which are all of the same type, e.g. string: > k <- c("bb", "bb", "bb", "aa", "cc", "cc") > and want to create a second vector containing the number of each entry > in k in the same order as in k, i.e. > c(3, 1, 2) > > or: > k <- c(5,5,5,5,2,2,4) > => c(4,2,1) > > thanks > > ______________________________________________ > 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. >