按时间顺序排列交叉表中的两位数年份

时间:2014-04-15 15:52:16

标签: r

我有一个交叉表(由table制作),行中的个人和列中的年份。为了节省水平空间,我想使用两位数而不是四位数年份(下游通过xtable进入LaTeX)。但是当我使用有序因子(通过ordered()factor(..., ordered=TRUE))时,我得到数字排序而不是按时间顺序排列。

我如何按时间顺序排列列?似乎当我将有序因子传递给table时,我失去了排序属性。

# representative data
dates <- as.Date("2010-01-01") - seq(30*365)
DF <- data.frame(day=sample(dates, 100, replace=TRUE),
                 id=sample(letters[1:5], 100, replace=TRUE))
DF$year <- as.numeric(format(DF$day, "%Y"))

# the table I want
table(DF$id, DF$year)

# but I'd like two year dates to save horizontal space
# but keep chronological order
DF <- DF[order(DF$day), ]
DF$yearShort <- factor(format(DF$day, "%y"), ordered=TRUE)

# but even though yearShort is ordered, the table isn't
is.ordered(DF$yearShort)
tab <- table(DF$id, DF$yearShort)
tab

# I can't order by rownames, either
tab[, order(dimnames(tab)[[2]])]

1 个答案:

答案 0 :(得分:1)

我对这个解决方案并不感到自豪,但它可以做你想做的事情:)

> tab2 <- table(DF$id, DF$year)
> colnames(tab2) <- substr(colnames(tab2),3,4)
> tab2

    80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 98 99 00 01 02 03 04 05 06 07 08 09
  a  0  2  0  2  2  2  0  1  0  2  0  1  0  2  2  0  1  0  1  0  0  1  1  1  0  1  1  1  1
  b  1  1  0  2  0  0  0  0  1  2  1  0  0  0  2  0  1  1  1  0  0  1  3  0  1  0  0  0  1
  c  3  1  0  1  0  0  1  0  1  0  0  0  0  0  1  1  0  0  0  0  1  1  0  1  1  0  0  2  0
  d  0  0  1  3  1  0  1  0  0  1  1  3  1  1  0  1  0  0  2  0  1  0  1  2  0  0  0  0  0
  e  1  0  2  0  3  1  0  0  1  0  0  3  1  0  0  0  0  2  1  1  1  0  1  0  1  1  1  0  0
相关问题