2016-09-18 12 views
0

我有這樣的數據集dput:ř擴展數據幀

structure(list(Account.Name = c("CMD", "CMD", "CMD", "CMD", "CMD", 
"CMD", "CMD", "CMD", "CMD", "CMD", "CMD", "CMD", "CMD", "CMD", 
"CMD", "CMD", "CMD", "CMD", "CMD", "CMD", "CMD", "CMD", "Colimbra", 
"Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", 
"Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", 
"Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", 
"Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", 
"Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", "Colimbra", 
"Colimbra"), Date.y = structure(c(47L, 38L, 39L, 46L, 29L, 30L, 
31L, 37L, 36L, 34L, 43L, 45L, 41L, 42L, 33L, 40L, 27L, 28L, 32L, 
35L, 44L, 26L, 9L, 24L, 17L, 23L, 18L, 6L, 8L, 5L, 12L, 10L, 
7L, 11L, 35L, 25L, 19L, 16L, 34L, 27L, 4L, 26L, 20L, 29L, 15L, 
33L, 32L, 30L, 14L, 22L, 31L, 13L, 21L, 28L), .Label = c("", 
"2012-12-01", "2013-01-01", "2013-02-01", "2013-03-01", "2013-04-01", 
"2013-05-01", "2013-06-01", "2013-07-01", "2013-08-01", "2013-09-01", 
"2013-10-01", "2013-11-01", "2013-12-01", "2014-01-01", "2014-02-01", 
"2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", 
"2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01", "2014-12-01", 
"2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", 
"2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01", "2015-10-01", 
"2015-11-01", "2015-12-01", "2016-01-01", "2016-02-01", "2016-03-01", 
"2016-04-01", "2016-05-01", "2016-06-01", "2016-07-01", "2016-08-01", 
"2016-09-01"), class = "factor"), EI = c(0.172413778757433, 0.283582069077747, 
0.304347804744803, 0.278195468486632, 0.675675653544559, 0.965738751378275, 
0.79789472055251, 0.571428546702807, 0.364238387240035, 0.333333310925928, 
0.333333310925928, 0.267175552791797, 0.30935249644739, 0.30935249644739, 
0.547169786306516, 0.342465730716834, 0.25581393431044, 0.593220290504169, 
0.529411739555941, 0.538461513372782, 0.333333310925928, 0.119266044513089, 
0.00689655157368212, 0.0932835783248028, 0.117967327490881, 0.111415832683409, 
0.0864661618980282, 0.0170648454887846, 0.0380999488912474, 0.00803673911715819, 
0.0500855092066307, 0.00942675138629104, 0.0201612894472413, 
0.0046082948309584, 0.00435339299151454, 0.144554447192982, 0.0830188645366324, 
0.0825861213183505, 0.0129474483080438, 0.0240963850193243, 0.00917431152659711, 
0.0215175530933231, 0.0953932023013541, 0.00917431172607524, 
0.0873239401148782, 0.00892174336861008, 0.018429689070739, 0.0352357312529589, 
0.0470588220329153, 0.059847657373831, 0.00588084875970071, 0.0479921625133198, 
0.229030327296333, 0.00613496919149197)), .Names = c("Account.Name", 
"Date.y", "EI"), row.names = c(69L, 70L, 71L, 72L, 73L, 74L, 
75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 
88L, 89L, 90L, 91L, 95L, 96L, 99L, 101L, 104L, 105L, 107L, 108L, 
109L, 110L, 111L, 113L, 114L, 116L, 117L, 118L, 119L, 120L, 121L, 
122L, 123L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 
134L), class = "data.frame") 

,我需要以這樣的方式來傳播它(它樞轉),每個行將以一個Account.Name和每列將是相關Date.y列的列名從0_date開始(如果它是該帳戶的最後一個Date.y日期值)並以i_date結尾(i是帳戶從開始計數到第一個日期記錄的索引) 。 例如:

Account.Name date_0,日期1,日期... CMD 0.333333311 0.333333311 0.309352496

  • 其中date_0對應於2016年6月1日 DATE_1對應於2016年5月1日 日期2對應於2016-04-01 依此類推...... 我試過用tidyr :: spread但是,列名被分配到了原始的日期值,而我想做一個相對的日期列名(從0_date開始計數,1_date直到每個賬戶的最後日期) 任何想法讚賞
+0

您能否顯示完整的預期輸出?你有沒有試過庫(reshape2); dcast(df1,Account.Name〜Date.y,value.var =「EI」)' – akrun

+0

如果我有完整的預期輸出,我不會在這裏。有沒有什麼具體的,你不明白在預期的產出? – RNN

+2

對不起,我不明白你想要什麼,因爲輸出輸出中的最小和最大日期是「2013-02-01」和「2016-09-01」。那麼'date_0'如何對應「2016-06-01」'。另外,如果您查看了其他可重複示例的帖子,其中大多數會提供預期的輸出(可能是通過手動創建它),以便其他人可以清楚地理解它。 – akrun

回答

0

設X是你的數據幀

library(data.table) 
library(lubridate) 
dt <- data.table(x) 
# date should not be factors 
dt[, Date.y := ymd(Date.y)] 
setorder(dt, Account.Name, -Date.y) 
dt[, col_index := 0:(.N-1L), by = Account.Name] 
dt_casted <- dcast(dt, Account.Name ~ col_index, value.var = "EI") 

注意,因爲我相信你會希望他們整理我沒有用「date_0」格式,而「date_10」將有錯誤的順序比較「日期2」 。更好地將索引保留爲數字或填充前導0.