stocks_wide <- data.frame(
time = as.Date('2009-01-01') + 0:9,
X = rnorm(10, 0, 1),
Y = rnorm(10, 0, 2),
Z = rnorm(10, 0, 4)
)
time | X | Y | Z |
---|---|---|---|
2009-01-01 | -0.9038382 | 2.9323136 | 1.0143806 |
2009-01-02 | -2.1347919 | 0.4645821 | 2.9855915 |
2009-01-03 | -0.9505768 | -0.4277255 | -6.4918586 |
2009-01-04 | -0.7117204 | -0.8850072 | -4.3852725 |
2009-01-05 | -1.0725914 | 0.6915813 | 0.2508038 |
2009-01-06 | -0.8038937 | 0.6023552 | 2.2062111 |
2009-01-07 | -0.9939167 | -1.1271743 | 2.4172513 |
2009-01-08 | 1.3823763 | 0.8452519 | -1.2894887 |
2009-01-09 | 1.1725629 | -2.5945300 | -2.8150979 |
2009-01-10 | 0.4646331 | -0.0330165 | -7.5588872 |
time | variable | value |
---|---|---|
2009-01-01 | X | -0.9038382 |
2009-01-02 | X | -2.1347919 |
2009-01-03 | X | -0.9505768 |
2009-01-04 | X | -0.7117204 |
2009-01-05 | X | -1.0725914 |
2009-01-06 | X | -0.8038937 |
2009-01-07 | X | -0.9939167 |
2009-01-08 | X | 1.3823763 |
2009-01-09 | X | 1.1725629 |
2009-01-10 | X | 0.4646331 |
2009-01-01 | Y | 2.9323136 |
2009-01-02 | Y | 0.4645821 |
2009-01-03 | Y | -0.4277255 |
2009-01-04 | Y | -0.8850072 |
2009-01-05 | Y | 0.6915813 |
2009-01-06 | Y | 0.6023552 |
2009-01-07 | Y | -1.1271743 |
2009-01-08 | Y | 0.8452519 |
2009-01-09 | Y | -2.5945300 |
2009-01-10 | Y | -0.0330165 |
2009-01-01 | Z | 1.0143806 |
2009-01-02 | Z | 2.9855915 |
2009-01-03 | Z | -6.4918586 |
2009-01-04 | Z | -4.3852725 |
2009-01-05 | Z | 0.2508038 |
2009-01-06 | Z | 2.2062111 |
2009-01-07 | Z | 2.4172513 |
2009-01-08 | Z | -1.2894887 |
2009-01-09 | Z | -2.8150979 |
2009-01-10 | Z | -7.5588872 |
stocks_wide %>%
gather(variable, value, -time) -> stocks_long
stocks_long %>%
spread(variable, value) %>%
print_table_long
time | X | Y | Z |
---|---|---|---|
2009-01-01 | -0.9038382 | 2.9323136 | 1.0143806 |
2009-01-02 | -2.1347919 | 0.4645821 | 2.9855915 |
2009-01-03 | -0.9505768 | -0.4277255 | -6.4918586 |
2009-01-04 | -0.7117204 | -0.8850072 | -4.3852725 |
2009-01-05 | -1.0725914 | 0.6915813 | 0.2508038 |
2009-01-06 | -0.8038937 | 0.6023552 | 2.2062111 |
2009-01-07 | -0.9939167 | -1.1271743 | 2.4172513 |
2009-01-08 | 1.3823763 | 0.8452519 | -1.2894887 |
2009-01-09 | 1.1725629 | -2.5945300 | -2.8150979 |
2009-01-10 | 0.4646331 | -0.0330165 | -7.5588872 |