I’m working through the examples in Kruschke’s Doing Bayesian Data Analysis and need a bit of help understanding how to get data into the format that his code examples require. In chapter 22 he has a table like this
Blue Brown Green Hazel
Black 20 68 5 15
Blond 94 7 16 10
Brunette 84 119 29 54
Red 17 26 14 14
I’m comfortable with inputting the table into R by entering it into a spreadsheet and using read.table("clipboard", header=T, sep="\t") or typing it into R like this
con.table2 <- matrix(c(20,68,5,15,94,7,16,10,84,119,29,54,17,26,14,14),nrow=4,byrow=TRUE)
dimnames(con.table2) <- list(c("Black","Blond","Brunette","Red"),c("Blue","Brown","Green","Hazel"))
But in his code, he presents this table like so, ready for analysis (full code is here http://www.indiana.edu/~kruschke/DoingBayesianDataAnalysis/Programs/PoissonExponentialJagsSTZ.R)
Freq = c(68,119,26,7,20,84,17,94,15,54,14,10,5,29,14,16)
Eye = c("Brown","Brown","Brown","Brown","Blue","Blue","Blue","Blue","Hazel" # runs off the page of his book
Hair = c("Black","Brunette","Red","Blond","Black","Brunette","Red","Blond","Black" # runs off the page of his book
It looks like the table has been converted into three vectors. What’s the most efficient way to do this? I’d like to replace his data with my own, so it would be great to learn how to transform the data into the format needed for this analysis.
For this, I’d use
melt()in thereshape2package: