I have created the following minimal example.
I want to turn this data frame (which is a melt so that there are three columns: Time, Room and ID)
structure(list(
Time = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
3, 3, 3, 3, 3, 3, 3),
Room = c("a", "a", "b", "b", "c", "c", "d", "d", "e", "e", "a", "a", "b", "b",
"c", "c", "d", "d", "e", "e", "a", "a", "b","b", "c", "c", "d", "d",
"e", "e"),
ID = c("A", NA, NA, NA, NA, NA, NA, "B", NA, NA, NA, NA, NA, "C", NA, "D",
NA, "E", NA, "F", NA, NA, NA, "G", NA, NA, NA, "H", NA, "I")),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, 30L),
.Names = c("Time", "Room", "ID"))
into this data frame
structure(
list(
Time = c(1, 2, 3),
a = c("A", NA, NA),
b = c(NA, "C", "G"),
c = c(NA, "D", NA),
d = c("B", "E", "H"),
e = c(NA, "F", "I")
),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -3L),
.Names = c("Time", "a", "b", "c", "d", "e")
)
which has the rooms as columns, the time as rows and the ID as entry.
I tried the following:
dcast(df, Time~Room, fun.aggregate=NULL, value.var='ID')
but this says: Aggregation function missing: defaulting to length
and doesn't return the ID value although the structure looks ok.
I also tried aggregate
but can't seem to know what to do.