fct_reorder factor column by another column
I have this dataframe called test
:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame")
I want to reorder event_type
according to hazard_ratio
, so I tried this to no avail..
test %>%
mutate(event_type = as.character(event_type),
event_type = fct_reorder(event_type, hazard_ratio))
r forcats
|
show 4 more comments
I have this dataframe called test
:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame")
I want to reorder event_type
according to hazard_ratio
, so I tried this to no avail..
test %>%
mutate(event_type = as.character(event_type),
event_type = fct_reorder(event_type, hazard_ratio))
r forcats
1
with(test, reorder(event_type, hazard_ratio))
– hrbrmstr
Nov 25 '18 at 19:52
Is there atidyverse
solution? Haha thank you
– JasonBaik
Nov 25 '18 at 19:54
If you're stuck with the tidyverse, thenmutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity))
. Look at the help forfct_reorder
, the.fun
is defined as.fun = median
.
– hrbrmstr
Nov 25 '18 at 19:54
Thank you!!!!!!!
– JasonBaik
Nov 25 '18 at 19:55
@hrbrmstr It still doesn't work. The order doesn't change..
– JasonBaik
Nov 25 '18 at 19:59
|
show 4 more comments
I have this dataframe called test
:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame")
I want to reorder event_type
according to hazard_ratio
, so I tried this to no avail..
test %>%
mutate(event_type = as.character(event_type),
event_type = fct_reorder(event_type, hazard_ratio))
r forcats
I have this dataframe called test
:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame")
I want to reorder event_type
according to hazard_ratio
, so I tried this to no avail..
test %>%
mutate(event_type = as.character(event_type),
event_type = fct_reorder(event_type, hazard_ratio))
r forcats
r forcats
asked Nov 25 '18 at 19:46
JasonBaikJasonBaik
350113
350113
1
with(test, reorder(event_type, hazard_ratio))
– hrbrmstr
Nov 25 '18 at 19:52
Is there atidyverse
solution? Haha thank you
– JasonBaik
Nov 25 '18 at 19:54
If you're stuck with the tidyverse, thenmutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity))
. Look at the help forfct_reorder
, the.fun
is defined as.fun = median
.
– hrbrmstr
Nov 25 '18 at 19:54
Thank you!!!!!!!
– JasonBaik
Nov 25 '18 at 19:55
@hrbrmstr It still doesn't work. The order doesn't change..
– JasonBaik
Nov 25 '18 at 19:59
|
show 4 more comments
1
with(test, reorder(event_type, hazard_ratio))
– hrbrmstr
Nov 25 '18 at 19:52
Is there atidyverse
solution? Haha thank you
– JasonBaik
Nov 25 '18 at 19:54
If you're stuck with the tidyverse, thenmutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity))
. Look at the help forfct_reorder
, the.fun
is defined as.fun = median
.
– hrbrmstr
Nov 25 '18 at 19:54
Thank you!!!!!!!
– JasonBaik
Nov 25 '18 at 19:55
@hrbrmstr It still doesn't work. The order doesn't change..
– JasonBaik
Nov 25 '18 at 19:59
1
1
with(test, reorder(event_type, hazard_ratio))
– hrbrmstr
Nov 25 '18 at 19:52
with(test, reorder(event_type, hazard_ratio))
– hrbrmstr
Nov 25 '18 at 19:52
Is there a
tidyverse
solution? Haha thank you– JasonBaik
Nov 25 '18 at 19:54
Is there a
tidyverse
solution? Haha thank you– JasonBaik
Nov 25 '18 at 19:54
If you're stuck with the tidyverse, then
mutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity))
. Look at the help for fct_reorder
, the .fun
is defined as .fun = median
.– hrbrmstr
Nov 25 '18 at 19:54
If you're stuck with the tidyverse, then
mutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity))
. Look at the help for fct_reorder
, the .fun
is defined as .fun = median
.– hrbrmstr
Nov 25 '18 at 19:54
Thank you!!!!!!!
– JasonBaik
Nov 25 '18 at 19:55
Thank you!!!!!!!
– JasonBaik
Nov 25 '18 at 19:55
@hrbrmstr It still doesn't work. The order doesn't change..
– JasonBaik
Nov 25 '18 at 19:59
@hrbrmstr It still doesn't work. The order doesn't change..
– JasonBaik
Nov 25 '18 at 19:59
|
show 4 more comments
1 Answer
1
active
oldest
votes
It sure looks like both methods reorder the factor on my system:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame") -> test
We'll use ggplot2
for verification since it uses factor
s for ordering axis things.
Original:
ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
Good 'ol base R
mutate(test, event_type = reorder(event_type, hazard_ratio)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
forcats
mutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
That's so weird! When I just run themutate
function without plotting, R shows me a data frame that has not changed, but once Iggplot
, it is ordered
– JasonBaik
Nov 25 '18 at 20:07
1
It reorders the factor. Do astr()
of the data frame or astr()
of the column before/after and you'll see that the factor changes. It's not going to rearrange the entire data frame. That's whatarrange()
does.
– hrbrmstr
Nov 25 '18 at 20:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53471222%2ffct-reorder-factor-column-by-another-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It sure looks like both methods reorder the factor on my system:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame") -> test
We'll use ggplot2
for verification since it uses factor
s for ordering axis things.
Original:
ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
Good 'ol base R
mutate(test, event_type = reorder(event_type, hazard_ratio)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
forcats
mutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
That's so weird! When I just run themutate
function without plotting, R shows me a data frame that has not changed, but once Iggplot
, it is ordered
– JasonBaik
Nov 25 '18 at 20:07
1
It reorders the factor. Do astr()
of the data frame or astr()
of the column before/after and you'll see that the factor changes. It's not going to rearrange the entire data frame. That's whatarrange()
does.
– hrbrmstr
Nov 25 '18 at 20:08
add a comment |
It sure looks like both methods reorder the factor on my system:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame") -> test
We'll use ggplot2
for verification since it uses factor
s for ordering axis things.
Original:
ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
Good 'ol base R
mutate(test, event_type = reorder(event_type, hazard_ratio)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
forcats
mutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
That's so weird! When I just run themutate
function without plotting, R shows me a data frame that has not changed, but once Iggplot
, it is ordered
– JasonBaik
Nov 25 '18 at 20:07
1
It reorders the factor. Do astr()
of the data frame or astr()
of the column before/after and you'll see that the factor changes. It's not going to rearrange the entire data frame. That's whatarrange()
does.
– hrbrmstr
Nov 25 '18 at 20:08
add a comment |
It sure looks like both methods reorder the factor on my system:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame") -> test
We'll use ggplot2
for verification since it uses factor
s for ordering axis things.
Original:
ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
Good 'ol base R
mutate(test, event_type = reorder(event_type, hazard_ratio)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
forcats
mutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
It sure looks like both methods reorder the factor on my system:
structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L,
7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL",
"HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio = c(0.909615543020822,
1.3191464689192, 0.979677208703559, 1.02474605962247, 1.04722377755438,
1.07656116782136, 1.01186162453814, 1.06021078216577, 0.972520062522276,
0.915937088175971)), row.names = c(NA, -10L), class = "data.frame") -> test
We'll use ggplot2
for verification since it uses factor
s for ordering axis things.
Original:
ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
Good 'ol base R
mutate(test, event_type = reorder(event_type, hazard_ratio)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
forcats
mutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity)) %>%
ggplot(aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))
answered Nov 25 '18 at 20:03
hrbrmstrhrbrmstr
61.4k691152
61.4k691152
That's so weird! When I just run themutate
function without plotting, R shows me a data frame that has not changed, but once Iggplot
, it is ordered
– JasonBaik
Nov 25 '18 at 20:07
1
It reorders the factor. Do astr()
of the data frame or astr()
of the column before/after and you'll see that the factor changes. It's not going to rearrange the entire data frame. That's whatarrange()
does.
– hrbrmstr
Nov 25 '18 at 20:08
add a comment |
That's so weird! When I just run themutate
function without plotting, R shows me a data frame that has not changed, but once Iggplot
, it is ordered
– JasonBaik
Nov 25 '18 at 20:07
1
It reorders the factor. Do astr()
of the data frame or astr()
of the column before/after and you'll see that the factor changes. It's not going to rearrange the entire data frame. That's whatarrange()
does.
– hrbrmstr
Nov 25 '18 at 20:08
That's so weird! When I just run the
mutate
function without plotting, R shows me a data frame that has not changed, but once I ggplot
, it is ordered– JasonBaik
Nov 25 '18 at 20:07
That's so weird! When I just run the
mutate
function without plotting, R shows me a data frame that has not changed, but once I ggplot
, it is ordered– JasonBaik
Nov 25 '18 at 20:07
1
1
It reorders the factor. Do a
str()
of the data frame or a str()
of the column before/after and you'll see that the factor changes. It's not going to rearrange the entire data frame. That's what arrange()
does.– hrbrmstr
Nov 25 '18 at 20:08
It reorders the factor. Do a
str()
of the data frame or a str()
of the column before/after and you'll see that the factor changes. It's not going to rearrange the entire data frame. That's what arrange()
does.– hrbrmstr
Nov 25 '18 at 20:08
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.
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%2f53471222%2ffct-reorder-factor-column-by-another-column%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
1
with(test, reorder(event_type, hazard_ratio))
– hrbrmstr
Nov 25 '18 at 19:52
Is there a
tidyverse
solution? Haha thank you– JasonBaik
Nov 25 '18 at 19:54
If you're stuck with the tidyverse, then
mutate(test, event_type = fct_reorder(event_type, hazard_ratio, .fun = identity))
. Look at the help forfct_reorder
, the.fun
is defined as.fun = median
.– hrbrmstr
Nov 25 '18 at 19:54
Thank you!!!!!!!
– JasonBaik
Nov 25 '18 at 19:55
@hrbrmstr It still doesn't work. The order doesn't change..
– JasonBaik
Nov 25 '18 at 19:59