2012-09-27 5 views
1

사소한 것을 놓치고 있어야하지만 아래의 캐스트가 데이터 프레임을 목록으로 변환하는 이유를 알 수 없습니다. 가능한 경우 출력을 데이터 프레임으로 만들고 싶습니다. 나는 그것을캐스팅은 데이터 프레임을 r (reshape package)의 목록으로 변환합니다. 데이터 프레임으로 유지하고 싶습니다.

d<-cast(d,Date+credit_id~varb) 

캐스팅하려고 내가

STR (D)

List of 4 
    $ Date  : Date[1:9], format: "2003-06-30" "2003-06-30" "2003-09-30" ... 
    $ credit_id: num [1:9] 12 18 12 18 12 ... 
    $ ebitda : num [1:9] NA NA NA NA NA ... 
    $ sales : num [1:9] 840 4831 854 4670 847 ... 
    - attr(*, "row.names")= int [1:9] 1 2 3 4 5 6 7 8 9 
    - attr(*, "idvars")= chr [1:2] "Date" "credit_id" 
    - attr(*, "rdimnames")=List of 2 
     ..$ :'data.frame': 9 obs. of 2 variables: 
     .. ..$ Date  : Date[1:9], format: "2003-06-30" "2003-06-30" "2003-09-30" ... 
     .. ..$ credit_id: num [1:9] 12 18 12 18 12 ... 
     ..$ :'data.frame': 2 obs. of 1 variable: 
     .. ..$ varb: chr [1:2] "ebitda" "sales" 
를 얻을 다음
str(d) 
    'data.frame': 12 obs. of 4 variables: 
    $ credit_id: num 12 12 12 12 18 ... 
    $ Date  : Date, format: "2003-06-30" "2003-09-30" "2003-12-31" ... 
    $ value : num 840 854 847 834 4831 ... 
    $ varb  : chr "sales" "sales" "sales" "sales" ... 

그것은 시작

전체 코드는 다음과 같습니다. 미리 감사드립니다.

d<-structure(list(credit_id = c(12, 12, 12, 12, 18, 18, 2073, 2073, 
    2103, 2103, 1776, 1776), Date = structure(c(12233, 12325, 12417, 
    12508, 12233, 12325, 15552, 15552, 15552, 15552, 15552, 15552 
    ), class = "Date"), value = c(839.8, 853.9, 846.9, 833.7, 4831.2, 
    4670, 54.1, 995, 90.944, 1092.8, 81.2, 1348.2), varb = c("sales", 
    "sales", "sales", "sales", "sales", "sales", "ebitda", "sales", 
    "ebitda", "sales", "ebitda", "sales")), .Names = c("credit_id", 
    "Date", "value", "varb"), row.names = c(606799L, 606800L, 606801L, 
    606802L, 606805L, 606806L, 1131814L, 1131822L, 1131950L, 1131958L, 
    1132034L, 1132042L), class = "data.frame") 
    head(d) 
    str(d) 
    d<-cast(d,Date+credit_id~varb) 
    head(d) 
    str(d) 
+0

'class (d)'는'list '의 특별한 형식 인'data.frame'이라는 것을 보여줍니다. 그러나, 그것은'cast_df' 클래스의 첫 번째 클래스이며, 그것은 자체 str 메소드를 가지고있어서 다른 것처럼 보입니다. 고맙습니다 @ 제임스. – James

+0

어쩌면 내가 그렇게 목록을 무서워해서는 안됩니다 :) – Aidan

답변

1

reshape2 패키지의 dcast을 사용하십시오. ?dcast에서 dcastdataframe을 제공함을 알 수 있습니다.

d 
    credit_id  Date  value varb 
1   14 2012-09-27 958.9766 ebitda 
2   14 2012-09-28 1024.3715 sales 
3   13 2012-09-29 1036.9162 ebitda 
4   11 2012-09-30 1028.7891 ebitda 
5   12 2012-10-01 984.7306 sales 
6   11 2012-10-02 1075.5891 sales 
7   11 2012-10-03 1019.4922 sales 
8   13 2012-10-04 968.9380 ebitda 
9   13 2012-10-05 889.2650 sales 
10  12 2012-10-06 1056.2465 ebitda 

d2 
     Date credit_id ebitda  sales 
1 2012-09-27  14 958.9766  NA 
2 2012-09-28  14  NA 1024.3715 
3 2012-09-29  13 1036.9162  NA 
4 2012-09-30  11 1028.7891  NA 
5 2012-10-01  12  NA 984.7306 
6 2012-10-02  11  NA 1075.5891 
7 2012-10-03  11  NA 1019.4922 
8 2012-10-04  13 968.9380  NA 
9 2012-10-05  13  NA 889.2650 
10 2012-10-06  12 1056.2465  NA 

다음 번에 귀하의 질문에 재현하기 위해 dput(dataframe)를 사용하여 일부 데이터를 제공해야합니다 :

set.seed(001) # Generating some data. 
credit_id <- sample(c(12,14,13,11), 10, TRUE) 
Date <- seq(Sys.Date(), length.out=10, by="1 day") 
value <- rnorm(10,1000,50) 
varb <- sample(c("ebitda", "sales"), 10, TRUE) 

d <- data.frame(credit_id, Date, value, varb) # this is something like your df 
str(d) 

'data.frame': 10 obs. of 4 variables: 
$ credit_id: num 14 14 13 11 12 11 11 13 13 12 
$ Date  : Date, format: "2012-09-27" "2012-09-28" ... 
$ value : num 959 1024 1037 1029 985 ... 
$ varb  : Factor w/ 2 levels "ebitda","sales": 1 2 1 1 2 2 2 1 2 1 

d2 <- dcast(d,Date+credit_id~varb) 
str(d2) 

'data.frame': 10 obs. of 4 variables: 
$ Date  : Date, format: "2012-09-27" "2012-09-28" ... 
$ credit_id: num 14 14 13 11 12 11 11 13 13 12 
$ ebitda : num 959 NA 1037 1029 NA ... 
$ sales : num NA 1024 NA NA 985 ... 

dd2는 다음과 같다.

+0

고마워요 @ 잭버, dcast에 대해 잘 알고 있습니다. 일을 재현 할 수 없게되어 죄송합니다. 나는 dput을 사용하여 결과를 d <-structure (list (credit_id = c (12, 12, 12, ...... 내 질문의 줄)에 복사했습니다. 나는 dput을 사용하는 방법이라고 생각했습니다. – Aidan

+1

예,'dput'을 사용하는 방법입니다. 저는 모양새가 reshape2의 구버전임을 잊지 않고 있습니다. [this] (http://stackoverflow.com/questions/12377334/) reshape-vs-reshape2-in-r) 몇 가지 설명을 구하십시오. 유용하게되어 기쁘다. –

관련 문제