如何将R数据帧转换为长格式

时间:2018-11-10 17:43:24

标签: r dataframe reshape tidyverse

我有数据框:

   Santa.Period            Index          Mean     Variance
1          TRUE          S&P 500 -5.463827e-05 5.552660e-05
2          TRUE              Dow  6.907256e-05 4.798628e-05
3          TRUE NASDAQ Composite -3.683476e-04 7.296956e-05
4          TRUE         FTSE 100  1.922876e-03 6.342067e-05
5          TRUE           CAC 40  1.223700e-03 9.531649e-05
6          TRUE              DAX  1.719576e-04 9.986086e-05
7         FALSE          S&P 500  2.488153e-04 1.676608e-04
8         FALSE              Dow  2.570371e-04 1.415451e-04
9         FALSE NASDAQ Composite  3.989929e-04 1.898479e-04
10        FALSE         FTSE 100  4.931637e-05 1.534737e-04
11        FALSE           CAC 40 -3.337471e-05 2.280848e-04
12        FALSE              DAX  1.916821e-04 2.142012e-04

我想对其进行重塑,以便为Santa.PeriodIndex的每种组合都有一列,并给出两行给出每种组合的均值和方差。

我已经整整几个小时不停地使用reshape的dcast,但是却无法到达任何地方。

我该如何解决?

1 个答案:

答案 0 :(得分:3)

我认为不需要dcast。您可以只使用df的转置,然后设置df的列名,粘贴IndexSanta.Period。试试:

setNames(data.frame(t(df[, -c(1, 2)])),
         paste(df$Index, df$Santa.Period, sep = "_"))
# output
          S&P 500_TRUE     Dow_TRUE NASDAQ Composite_TRUE FTSE 100_TRUE  CAC 40_TRUE     DAX_TRUE S&P 500_FALSE    Dow_FALSE NASDAQ Composite_FALSE FTSE 100_FALSE  CAC 40_FALSE    DAX_FALSE
Mean     -5.463827e-05 6.907256e-05         -3.683476e-04  1.922876e-03 1.223700e-03 1.719576e-04  0.0002488153 0.0002570371           0.0003989929   4.931637e-05 -3.337471e-05 0.0001916821
Variance  5.552660e-05 4.798628e-05          7.296956e-05  6.342067e-05 9.531649e-05 9.986086e-05  0.0001676608 0.0001415451           0.0001898479   1.534737e-04  2.280848e-04 0.0002142012

数据

df <- structure(list(Santa.Period = c(TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), Index = c("S&P 500", 
"Dow", "NASDAQ Composite", "FTSE 100", "CAC 40", "DAX", "S&P 500", 
"Dow", "NASDAQ Composite", "FTSE 100", "CAC 40", "DAX"), Mean = c(-5.463827e-05, 
6.907256e-05, -0.0003683476, 0.001922876, 0.0012237, 0.0001719576, 
0.0002488153, 0.0002570371, 0.0003989929, 4.931637e-05, -3.337471e-05, 
0.0001916821), Variance = c(5.55266e-05, 4.798628e-05, 7.296956e-05, 
6.342067e-05, 9.531649e-05, 9.986086e-05, 0.0001676608, 0.0001415451, 
0.0001898479, 0.0001534737, 0.0002280848, 0.0002142012)), .Names = c("Santa.Period", 
"Index", "Mean", "Variance"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))