fct_reorder factor column by another column












1















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))









share|improve this question


















  • 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 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











  • @hrbrmstr It still doesn't work. The order doesn't change..

    – JasonBaik
    Nov 25 '18 at 19:59
















1















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))









share|improve this question


















  • 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 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











  • @hrbrmstr It still doesn't work. The order doesn't change..

    – JasonBaik
    Nov 25 '18 at 19:59














1












1








1








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))









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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











  • 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





    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 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











  • @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












1 Answer
1






active

oldest

votes


















0














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 factors for ordering axis things.



Original:



ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))


enter image description here



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))


enter image description here



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))


enter image description here






share|improve this answer
























  • 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





    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











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
});


}
});














draft saved

draft discarded


















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









0














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 factors for ordering axis things.



Original:



ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))


enter image description here



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))


enter image description here



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))


enter image description here






share|improve this answer
























  • 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





    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
















0














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 factors for ordering axis things.



Original:



ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))


enter image description here



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))


enter image description here



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))


enter image description here






share|improve this answer
























  • 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





    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














0












0








0







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 factors for ordering axis things.



Original:



ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))


enter image description here



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))


enter image description here



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))


enter image description here






share|improve this answer













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 factors for ordering axis things.



Original:



ggplot(test, aes(hazard_ratio, event_type)) +
geom_segment(aes(xend=0, yend=event_type))


enter image description here



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))


enter image description here



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))


enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 20:03









hrbrmstrhrbrmstr

61.4k691152




61.4k691152













  • 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





    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



















  • 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





    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

















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python