2014-09-15 4 views
-4

데이터 프레임, 내가 원하는 R 내가 아래에 언급 한 바와 같이하는 데이터 프레임 ema_sma_actual이

fiscal_quarter  actual forecast_ema forecast_sma 
     20071 6371428347   NA   NA 
     20072 4370623177   NA   NA 
     20073 3377139334 5056396953 5063969536 
     20074 8380921297 6953659125 5762279366 
     20081 7315765174 7054712149 6579419356 
     20082 11325882146 9155297148 9075228726 
     20083 6311248678 7653272913 8176319996 

내가이 작업을 수행 할 수있는 방법을

type    Amount    fiscal_quarter 
     actual 6371428347   20071 
    forecast_ema   NA    20071 
    forecast_sma   NA    20071 
     actual 3377139334   20072 
    forecast_ema 5056396953   20072 
    forecast_sma 5063969536   20072 
     actual 8380921297   20073 
    forecast_ema 6953659125   20073 
    forecast_sma 5762279366   20073 
     actual 7315765174   20074 
forecast_ema 7054712149   20074 
forecast_sma 6579419356   20074 
     actual 11325882146   20081 
forecast_ema 9155297148   20081 
forecast_sma 9075228726   20081 
     actual 6311248678   20082 
forecast_ema 7653272913   20082 
forecast_sma 8176319996   20082 
     actual 7356638637   20083 
forecast_ema 7559955775   20083 
forecast_sma 8312564876   20083 

로 변환 할 수있는 모양을 변경할?

+1

하는'라이브러리 (reshape2)를보십시오; df2 <- melt (df, "fiscal_quarter"); df2 [order (df2 $ fiscal_quarter),]'. 'df'가 데이터 세트라고 가정하면 –

+0

정말 고마워요. .. –

+0

NP, 편집을 되 돌리는 것을 멈추십시오. –

답변

1

tidyr 솔루션

당신이 위의 순서로하려면
df<-read.table(text="fiscal_quarter  actual forecast_ema forecast_sma 
20071 6371428347   NA   NA 
20072 4370623177   NA   NA 
20073 3377139334 5056396953 5063969536 
20074 8380921297 6953659125 5762279366 
20081 7315765174 7054712149 6579419356 
20082 11325882146 9155297148 9075228726 
20083 6311248678 7653272913 8176319996", header=T) 


library(tidyr) 
library(dplyr) 

df %>% gather(key,amount, 2:4) 

, 당신은 추가 할 수 있습니다

df %>% 
    gather(key,amount, 2:4) %>% 
    arrange(fiscal_quarter, key) 
관련 문제