How to get R dataframe into long format
up vote
1
down vote
favorite
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?
r dataframe reshape tidyverse
add a comment |
up vote
1
down vote
favorite
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?
r dataframe reshape tidyverse
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
r dataframe reshape tidyverse
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?
r dataframe reshape tidyverse
r dataframe reshape tidyverse
edited Nov 10 at 17:52
Ali Khaki
7011418
7011418
asked Nov 10 at 17:43
Henry
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
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"))
Thank you. Really appreciate your help.
– Henry
Nov 10 at 18:05
You're welcome @Henry. Also welcome to SO! If you want to order you final data frame (df2
) following column names you can usedf2[, order(names(df2))]
– ANG
Nov 10 at 18:09
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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"))
Thank you. Really appreciate your help.
– Henry
Nov 10 at 18:05
You're welcome @Henry. Also welcome to SO! If you want to order you final data frame (df2
) following column names you can usedf2[, order(names(df2))]
– ANG
Nov 10 at 18:09
add a comment |
up vote
3
down vote
accepted
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"))
Thank you. Really appreciate your help.
– Henry
Nov 10 at 18:05
You're welcome @Henry. Also welcome to SO! If you want to order you final data frame (df2
) following column names you can usedf2[, order(names(df2))]
– ANG
Nov 10 at 18:09
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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"))
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"))
answered Nov 10 at 18:01
ANG
4,1312620
4,1312620
Thank you. Really appreciate your help.
– Henry
Nov 10 at 18:05
You're welcome @Henry. Also welcome to SO! If you want to order you final data frame (df2
) following column names you can usedf2[, order(names(df2))]
– ANG
Nov 10 at 18:09
add a comment |
Thank you. Really appreciate your help.
– Henry
Nov 10 at 18:05
You're welcome @Henry. Also welcome to SO! If you want to order you final data frame (df2
) following column names you can usedf2[, order(names(df2))]
– ANG
Nov 10 at 18:09
Thank you. Really appreciate your help.
– Henry
Nov 10 at 18:05
Thank you. Really appreciate your help.
– Henry
Nov 10 at 18:05
You're welcome @Henry. Also welcome to SO! If you want to order you final data frame (
df2
) following column names you can use df2[, order(names(df2))]
– ANG
Nov 10 at 18:09
You're welcome @Henry. Also welcome to SO! If you want to order you final data frame (
df2
) following column names you can use df2[, order(names(df2))]
– ANG
Nov 10 at 18:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241714%2fhow-to-get-r-dataframe-into-long-format%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown