I am struggling with a reshape package looking for a way to "distinguish" a data frame, but with two (or more) values โโin "value.var".
Here is 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 data framework:
tx <- ddply(df, c('ExamenYear','StudentGender'), summarize, participated = sum(participated == "yes"), passed = sum(passed == "yes"))
In the logic of change, I have two variable values " and passed
I am looking for a way to combine the following information in a single data frame:
dcast(tx, formula = ExamenYear ~ StudentGender, value.var = 'participated') dcast(tx, formula = ExamenYear ~ StudentGender, value.var = 'passed')
The final table I'm trying to create will 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 two "variable values" in the translation function?