Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

Using R or Excel, what is the easiest way to convert a frequency table into a vector of values?

E.g., How would you convert the following frequency table

Value   Frequency
  1.      2
  2.      1
  3.      4
  4.      2
  5.      1

into the following vector?

1, 1, 2, 3, 3, 3, 3, 4, 4, 5
share|improve this question

2 Answers

up vote 10 down vote accepted

In R, you can do it using the repcommand:

tab <- data.frame(value=c(1, 2, 3, 4, 5), freq=c(2, 1, 4, 2, 1))
vec <- rep(tab$value, tab$freq)

This gives following result:

> tab
  value freq
1     1    2
2     2    1
3     3    4
4     4    2
5     5    1

> vec
 [1] 1 1 2 3 3 3 3 4 4 5

For details, see the help file for the repcommand by typing ?rep.

share|improve this answer
1  
Or, given a simple frequency table for vec <- sample(1:5, 20, TRUE) created by table(): rep(names(table(vec)), table(vec)). The result then needs to be converted with as.numeric() if the categories are actual numbers. – caracal Sep 15 '11 at 6:40

Obviously in R it's simpler.

In Excel I would use a helper column (if the value is in A1):

  value freq help
1     1    2  =REPT(A2 & ", ",B2)
2     2    1  =C1 & REPT(A3 & ", ",B3)
3     3    4  (drag or copy from upper cell)
4     4    2  (drag or copy from upper cell)
5     5    1  (drag or copy from upper cell)
              =LEFT(C6, LEN(C6)-1)

In C7 you have your result

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.