2014-04-15 2 views
0

저는 열과 행의 개인이있는 십자가 테이블 (table으로 작성)을 가지고 있습니다. 수평 공간을 절약하기 위해 4 자리 연도가 아닌 2 자리 연도를 사용하고 싶습니다 (다운 스트림은 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

나는 해결책을 매우 자랑스럽게 아니에요하지만 당신이 할 :)

> 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 
+0

샤프을 원하는 않습니다. 이것은 유일한 해결책 일 수 있습니다. –

관련 문제