0

I have data table like this

year       value
2010        25          
2011       168          
2012        48          
2010       189          
2011       192          
2012       38           
2010       175          
2011       55           
2012       48   

I want to distinguish my data to be like this

year       value

2010        3 (25 189 175: = 33.33% )
2011        3 (168 192 55 : = 33.33%)
2012        3 (48 38 48: = 33.33%)

for further plotting bar graph which have 3 main groups (2010 2011 2012) in X-axis and % of members in each year in Y-axis

What should I do? I'm a beginner in R program. Thank you in advanced :D

2 Answers2

0

Do you want this

> data.frame(xtabs(~year, df))
  year Freq
1 2010    3
2 2011    3
3 2012    3

or the plot

barplot(prop.table(xtabs(~year, df)))

enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

You should note that for plotting the graph, the transformation you describe is not necessary. But you can do

dplyr::count(your_data, year)

returning

# A tibble: 3 x 2
   year     n
  <dbl> <int>
1  2010     3
2  2011     3
3  2012     3
ktiu
  • 2,606
  • 6
  • 20