Thanks to @akrun who pointed it out.
Well, there's a high chance that your data has duplicate row that look either like this:
student test score
Adam Exam1 80
Adam Exam1 85
Adam Exam2 90
John Exam1 70
John Exam2 60
Or like this:
student class test score
Adam Biology Exam1 80
Adam Theology Exam1 85
Adam Theology Exam2 90
John Biology Exam1 70
John Theology Exam2 60
When you cast it like this: dcast(data, student + class ~ test, value.var='score')