Search code examples
raggregatebar-chart

Barplot in R, aggregation of sampled data


I want a stacked barplot or at least two barplots (histograms) of the data below. But I can't figure out how. plot(online) is not the solution, I´m looking for. Please see below.

          online              offline
1         sehrwichtig             wichtig
2             wichtig           unwichtig
3         sehrwichtig           unwichtig
4         sehrwichtig         sehrwichtig
5         sehrwichtig         sehrwichtig
6         sehrwichtig           unwichtig
7         sehrwichtig           unwichtig
8             wichtig             wichtig
9             wichtig           unwichtig
10        sehrwichtig         sehrwichtig
11        sehrwichtig             wichtig
12        sehrwichtig           unwichtig
13            wichtig         sehrwichtig
14        sehrwichtig             wichtig

I know I need a step, where the data is aggregated to:

                   online        offline 
   sehrwichtig           6         7 
   unwichtig             0         1 
   wichtig               3         5 

But how?


Solution

  • That aggregation is just a simple call to table inside of apply:

    R> foo <- data.frame(online=sample(c("S","W","U"),10,TRUE), 
                         offline=sample(c("S","W","U"),10,TRUE))
    R> apply(foo,2,table)
      online offline
    S      3       1
    U      4       5
    W      3       4
    

    which you can feed into barplot.