In R and several other programs, this process is referred to as "reshaping" data. In fact, the Tableau page that you originally linked to speaks of their "Excel Reshaper plugin".
In base R, there are a few functions to reshape data, such as the (notorious) reshape()
function which takes panel data from a wide form to a long form, and stack()
which creates skinny stacks of your data.
The "reshape2" package seems to be much more popular for such data transformations, though.
Here's an example of "melting" your sample data, which I've stored in a data.frame
named "mydf":
library(reshape2)
melt(mydf, id.vars=c("ID", "Gender", "School"),
value.name="Score", variable.name="Subject")
# ID Gender School Subject Score
# 1 1 M West Math 90
# 2 2 F South Math 50
# 3 1 M West English 80
# 4 2 F South English 50
# 5 1 M West Science 70
# 6 2 F South Science 50
For this example, base R's reshape()
isn't really appropriate, but stack()
is. Here, I've stack
ed just the last three columns:
stack(mydf[4:6])
# values ind
# 1 90 Math
# 2 50 Math
# 3 80 English
# 4 50 English
# 5 70 Science
# 6 50 Science
To get the data.frame
you are looking for, you would cbind
the first three columns with the above output.
For reference, Hadley Wickham's Tidy Data paper is a good entry point into thinking about how the structure of your data might facilitate further processing and visualization.