Note that there are some explanatory texts on larger screens.

plurals
  1. POPlotting data grouped by pairs of variables
    text
    copied!<p>I have a large set of data like this consisting of the following variables.</p> <pre><code>Field Country AgeRange Score Test </code></pre> <p>I would like to barplot the average score of each group of all population grouped by Field and AgeRange. That is, I'd like something like this.</p> <p><img src="https://i.stack.imgur.com/Q8tbw.png" alt="enter image description here"></p> <p>Note that variable AgeRange takes one of those 3 values, not the exact age of each participant. </p> <p>I have no problem to group the data as needed. For example, by doing</p> <pre><code>aggr_data = aggregate(data, by=list(data$Field, data$AgeRange), FUN=mean) </code></pre> <p>I get the data grouped the way I needed, with a Score average associated to each Field-AgeRange pair. The problem is that I can't find a straightforward way to get a barplot from those values in which the y-axis corresponds to the obtained score, and the x-axis to each pair.</p> <p>I think I could just grab each of the subsets I'm interested in, like so</p> <pre><code>young_cs = subset(data, Field=="CompSci" &amp; AgeRange=="18-35") m_young_cs = mean(young_cs[,"Score"]) mid_cs = subset(data, Field=="CompSci" &amp; AgeRange=="36-53") m_mid_cs = mean(mid_cs[,"Score"]) </code></pre> <p>and then plot all the obtained means, but this is obviously very time-consuming. Is there a simpler, faster way to do this?</p> <p>Here's a small random sample of the data.</p> <pre><code>Field Country AgeRange Score Test Psychology US 18-35 4.2 A Psychology US 18-35 3.1 C Psychology US 18-35 5.2 B Psychology US 36-53 4.7 A Psychology US 36-53 3.5 A Psychology US 54+ 3.1 B Psychology US 54+ 2.2 B Psychology US 54+ 6.7 C Psychology US 54+ 5.1 C CompSci US 18-35 5.2 B CompSci US 18-35 7.4 C CompSci US 18-35 6.1 A CompSci US 36-53 7.7 A CompSci US 36-53 8.1 A CompSci US 54+ 8.2 B CompSci US 54+ 7.7 B CompSci US 54+ 6.9 A CompSci US 54+ 9.0 C Mathematics US 18-35 6.2 B Mathematics US 18-35 6.4 A Mathematics US 18-35 7.1 A Mathematics US 36-53 8.7 A Mathematics US 36-53 9.4 A Mathematics US 54+ 7.2 C Mathematics US 54+ 6.1 B Mathematics US 54+ 6.5 C Mathematics US 54+ 7.0 C </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload