zhenjiang xu
2011-Aug-31 16:45 UTC
[R] how to create data.frames from vectors with duplicates
Hi R users, suppose I have two vectors, > x=c(1,2,3,4,5) > y=c('a','b','c','a','c') How can I get a data.frame like this?> xycount a 5 b 2 c 8 I know a few ways to fulfill the task. However, I have a huge number of this kind calculations, so I'd like an efficient solution. Thanks -- Best, Zhenjiang
Jorge I Velez
2011-Aug-31 16:50 UTC
[R] how to create data.frames from vectors with duplicates
Hi Zhenjiang, Try table(unlist(mapply(function(x, y) rep(x, y), y, x))) HTH, Jorge On Wed, Aug 31, 2011 at 12:45 PM, zhenjiang xu <> wrote:> Hi R users, > > suppose I have two vectors, > > x=c(1,2,3,4,5) > > y=c('a','b','c','a','c') > How can I get a data.frame like this? > > xy > count > a 5 > b 2 > c 8 > > I know a few ways to fulfill the task. However, I have a huge number > of this kind calculations, so I'd like an efficient solution. Thanks > > -- > Best, > Zhenjiang > > ______________________________________________ > 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]]
Henrique Dallazuanna
2011-Aug-31 16:55 UTC
[R] how to create data.frames from vectors with duplicates
Try this: rowsum(x, y) On Wed, Aug 31, 2011 at 1:45 PM, zhenjiang xu <zhenjiang.xu at gmail.com> wrote:> > Hi R users, > > suppose I have two vectors, > ?> x=c(1,2,3,4,5) > ?> y=c('a','b','c','a','c') > How can I get a data.frame like this? > > xy > ? ? ?count > a ? ? 5 > b ? ? 2 > c ? ? 8 > > I know a few ways to fulfill the task. However, I have a huge number > of this kind calculations, so I'd like an efficient solution. Thanks > > -- > Best, > Zhenjiang > > ______________________________________________ > 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
Marc Schwartz
2011-Aug-31 16:58 UTC
[R] how to create data.frames from vectors with duplicates
On Aug 31, 2011, at 11:45 AM, zhenjiang xu wrote:> Hi R users, > > suppose I have two vectors, >> x=c(1,2,3,4,5) >> y=c('a','b','c','a','c') > How can I get a data.frame like this? >> xy > count > a 5 > b 2 > c 8 > > I know a few ways to fulfill the task. However, I have a huge number > of this kind calculations, so I'd like an efficient solution. ThanksSee ?rep and ?as.data.frame.table Try this:> data.frame(table(rep(y, x)))Var1 Freq 1 a 5 2 b 2 3 c 8 HTH, Marc Schwartz
Maybe Matching Threads
- counting the duplicates in an object of list
- how to reshape the data.frame from long to wide in a specific order
- how to add two data.frame with the same column but different row numbers
- a question on list manipulation
- how to control to save plots to which dev