1

I have the following code to generate a dataset in R. However, I want the actual precise values for each x value in the chart and cant figure out how to generate these values. Here is the code I have:

x <- c("CF", "CH", "CJ", "CE", "CN", "EC", "EN", "EJ", "AB", "BA", "KO", "OD", "DL", "HG")
px = c(0.08, 0.10, 0.06, 0.20, 0.04, 0.15, 0.02, 0.10, 0.025, 0.025, 0.05, 0.05, 0.05, 0.05)
draws = sample(x, size = 1000, replace = TRUE, prob = px)
barplot(height=table(draws))

How can I get the precise frequencies for each of the x values (i.e. CF, CH, CE, etc.)?

amber4478
  • 6,433
  • 3
  • 20
  • 17
  • What do you mean by "How to generate these values"? You _are_ 'generating' them by `table(draws)`? Or am I misunderstanding something? Do you mean how to add the values to each bar in the plot? – Henrik Feb 13 '14 at 19:01

2 Answers2

2

The function 'table' provides counts of each factor

Try: table(draws)

habd
  • 158
  • 1
  • 8
0

Here is one way:

sapply(lapply(x, '==', draws), sum)
user1981275
  • 13,002
  • 8
  • 72
  • 101