I have a data frame containing the following fields: a, b, c. a and b are identifiers and c is a Date. Not all identifier combinations have a date. There are some duplicate (a,b)s in the data. I only need the last c.
I want to create a table where the levels of a form the rows and the levels of b form the columns. If there is a c matching the levels of a and b, it should end up in the corresponding cell (t[a,b] = c). (I want to cluster the events with the table as a basis for a distance matrix.)
I tried doing the following:
f <- function(x) {
if (length(x) > 0) {
return(x[length(x)])
}
else {
return(NA)
}
}
m.df <- melt(df)
c.df <- cast(m.df, a ~ b, fun.aggregate = f)
This is otherwise OK, but cast somehow mangles the Dates into integers (14746 and whatnot). Why does this happen? Everything seems to be fine inside f. I can always convert the columns back into Dates, but this is rather strange – a bug?
Take a look at
?matrix. Specifically this paragraph in the Details section:Dateis not in that list, so you just get the underlying integer values.