Reputation: 33
I have the dataframe:
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
I would like to reshape it so that there is a column for each combination of Santa.Period
and Index
and two rows giving each combination's mean and variance.
I have been fiddling about with reshape's dcast
for hours, but haven't been able to get anywhere.
How can I solve this?
Upvotes: 1
Views: 42
Reputation: 6768
No need for dcast
I think. You can just use the transpose of df
then set df
column names pasting Index
and Santa.Period
. Try out:
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
Data
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"))
Upvotes: 3