I have what should be a simple reshaping problem, but I can't figure it out. Part of my data looks like this:
foo <- structure(list(grade = c(3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
3, 3, 4, 4, 5, 5, 6, 6), var.type = structure(c(3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L
), .Label = c("Raw Score", "SE", "SS"), class = "factor"), var.val = c(120L,
47L, 120L, 46L, 120L, 46L, 120L, 47L, 120L, 46L, 120L, 46L, 120L,
12L, 120L, 14L, 120L, 16L, 120L, 20L)), .Names = c("grade", "var.type",
"var.val"), row.names = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L), class = "data.frame")
or
grade var.type var.val
2 3 SS 120
3 3 SE 47
4 4 SS 120
5 4 SE 46
6 5 SS 120
7 5 SE 46
I want to make it look like this:
grade SS SE
3 120 47
4 120 46
5 120 46
and so on. I have tried reshape, cast, and dcast as in this thread:
but nothing seems to work. I would really appreciate some help. TIA.
If you don't have any duplicates, this will work nicely:
If you want to reshape and you have duplicates, you're going to need to give each pair a unique id:
Now you'll be able to use reshape properly:
EDIT: Please note I'm assuming your data is in order, else you'll have problems distinguishing between duplicates. If it isn't, you can always use
order
so that it is.It is not as pretty as reshape, but
produces
You have to assume the data comes in pairs of rows for this.