我試圖複製一個數據框(動物園對象)50次作爲一個整體,並得到結果作爲矩陣,但我所嘗試的所有命令似乎不成功。我可以很容易地編寫一個能夠做到這一點的函數,但我希望使用rep
可以輕鬆實現結果。n次複製一個數據幀
考慮以下作爲一個例子
x <- zoo(data.frame(A = c(1,2,3,4,5,6), B = c(7,8,9,10,11,12), C = c(13,14,15,16,17,18)), order.by = seq(as.Date("2012-01-01"), as.Date("2012-06-01"), by = "month"))
#> x
# A B C
#2012-01-01 1 7 13
#2012-02-01 2 8 14
#2012-03-01 3 9 15
#2012-04-01 4 10 16
#2012-05-01 5 11 17
#2012-06-01 6 12 18
讓我們只是試圖複製x
2倍。最終的結果我找的是:
# [,1] [,2] [,3]
# [1,] 1 7 13
# [2,] 2 8 14
# [3,] 3 9 15
# [4,] 4 10 16
# [5,] 5 11 17
# [6,] 6 12 18
# [7,] 1 7 13
# [8,] 2 8 14
# [9,] 3 9 15
#[10,] 4 10 16
#[11,] 5 11 17
#[12,] 6 12 18
這是我到目前爲止已經試過,但這些工作:
matrix(rep(x,2), ncol = 3, byrow = T)
OR
matrix(rep(x,2), ncol = 3, byrow = F)
OR
matrix(rep(x, each = 2), ncol = 3)
任何人都可以幫忙嗎?
謝謝,
看一看[@ Joran的答案在這裏(http://stackoverflow.com/questions/11693599/alternative-to-expand-grid-for-data-frames) – Henrik