Casting a Date matrix?

陌路散爱 提交于 2019-12-11 13:48:54

问题


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?


回答1:


Take a look at ?matrix. Specifically this paragraph in the Details section:

 ‘as.matrix’ is a generic function.  The method for data frames
 will return a character matrix if there is any
 non-(numeric/logical/complex) column, applying ‘format’ to
 non-character columns.  Otherwise, the usual coercion hierarchy
 (logical < integer < double < complex) will be used, e.g.,
 all-logical data frames will be coerced to a logical matrix, mixed
 logical-integer will give a integer matrix, etc.

Date is not in that list, so you just get the underlying integer values.



来源:https://stackoverflow.com/questions/9116490/casting-a-date-matrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!