Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Generate histogram from counts of data

Tags:

r

Suppose I have vector a:

c(1, 6, 2, 4.1, 1, 2)

And a count vector b:

c(2,3,2,1,1,0)

I'd like to generate vector c:

c(1, 1, 6, 6, 6, 2, 2, 4.1, 1)

To call:

hist(c)

How can I build c, or is there a way to generate the histogram directly from a and b? Note the duplicates in a, as well as unequal spacing.

Require a vectorized solution. a and b are too large for lapply and friends.

like image 812
Clayton Stanley Avatar asked Nov 20 '12 01:11

Clayton Stanley


1 Answers

?rep

> rep(a, b)
[1] 1.0 1.0 6.0 6.0 6.0 2.0 2.0 4.1 1.0
> 

Edit since I was curious!

a <- sample(1:10, 1e6, replace=TRUE)
b <- sample(1:10, 1e6, replace=TRUE)

> system.time(rep(a, b))
   user  system elapsed 
  0.140   0.016   0.156 
> system.time(inverse.rle(list(lengths=b, values=a)))
   user  system elapsed 
  0.024   0.004   0.028 
like image 58
Justin Avatar answered Oct 04 '22 23:10

Justin