I am struggling with reshape package looking for a way “cast” a dataframe but with two (or more) values in “value.var”.
Here an example of what I want to achieve.
df <- data.frame( StudentID = c("x1", "x10", "x2",
"x3", "x4", "x5", "x6", "x7", "x8", "x9"),
StudentGender = c('F', 'M', 'F', 'M', 'F', 'M', 'F', 'M', 'M', 'M'),
ExamenYear = c('2007','2007','2007','2008','2008','2008','2008','2009','2009','2009'),
Exam = c('algebra', 'stats', 'bio', 'algebra', 'algebra', 'stats', 'stats', 'algebra', 'bio', 'bio'),
participated = c('no','yes','yes','yes','no','yes','yes','yes','yes','yes'),
passed = c('no','yes','yes','yes','no','yes','yes','yes','no','yes'),
stringsAsFactors = FALSE)
From df I can create the following dataframe :
tx <- ddply(df, c('ExamenYear','StudentGender'), summarize,
participated = sum(participated == "yes"),
passed = sum(passed == "yes"))
In the reshape logic, I have two “value variables” participated and passed
I am looking for way to combine – in one dataframe – the following information :
dcast(tx, formula = ExamenYear ~ StudentGender, value.var = 'participated')
dcast(tx, formula = ExamenYear ~ StudentGender, value.var = 'passed')
The end table I am trying to create would look like this
tempTab1 <- dcast(tx, formula = ExamenYear ~ StudentGender, value.var = 'participated')
tempTab2 <- dcast(tx, formula = ExamenYear ~ StudentGender, value.var = 'passed')
as.data.frame(cbind(ExamenYear = tempTab1[,1],
Female_Participated = tempTab1[,2],
Female_Passed = tempTab2[,2],
Male_Participated = tempTab1[,3],
Male_Passed = tempTab2[,3]
))
Is it possible to have have two "value variables" in a cast function ?