The logic behind `reorder()` in `R`?












2















I have difficulties understanding the logic behind reorder().



Suppose Var is defined as below:



Var <- factor(c(0.2, 0.1, -0.1))
order(Var)


Now if I want to reorder it to c(1, 2, 3) I would run the following code, which works perfectly fine.



Needed_Order <- c(1, 2, 3)
Var <- reorder(Var, Needed_Order)
order(Var)


But it does not work if I want to reorder Var to c(3, 1, 2)



Needed_Order <- c(3,1,2)
Var <- reorder(Var, Needed_Order)
order(Var)


I expect to get 3 1 2 as the output of order(var) but it returns 2 3 1.










share|improve this question


















  • 1





    Why do you expect to get that? If you want to reorder the levels, you should supply the actual levels. If you read the help file for reorder (?reorder) you will see bymedian <- with(InsectSprays, reorder(spray, count, median)) which shows you that you don't supply an array of nubers, you have to supply a function that will be used for the ordering.

    – Elin
    Nov 22 '18 at 4:09











  • @Elin so what function should I use if I want a particular order, which cannot be defined by a function? Note that I do not want to supply the actual levels, as there are so many of them and I need to repeat this process many times

    – Milad
    Nov 22 '18 at 4:15











  • Well what is the order you want? If it's not the levels themselves then you will need to provide some other values that are ***actually found in the data ***

    – Elin
    Nov 22 '18 at 4:27
















2















I have difficulties understanding the logic behind reorder().



Suppose Var is defined as below:



Var <- factor(c(0.2, 0.1, -0.1))
order(Var)


Now if I want to reorder it to c(1, 2, 3) I would run the following code, which works perfectly fine.



Needed_Order <- c(1, 2, 3)
Var <- reorder(Var, Needed_Order)
order(Var)


But it does not work if I want to reorder Var to c(3, 1, 2)



Needed_Order <- c(3,1,2)
Var <- reorder(Var, Needed_Order)
order(Var)


I expect to get 3 1 2 as the output of order(var) but it returns 2 3 1.










share|improve this question


















  • 1





    Why do you expect to get that? If you want to reorder the levels, you should supply the actual levels. If you read the help file for reorder (?reorder) you will see bymedian <- with(InsectSprays, reorder(spray, count, median)) which shows you that you don't supply an array of nubers, you have to supply a function that will be used for the ordering.

    – Elin
    Nov 22 '18 at 4:09











  • @Elin so what function should I use if I want a particular order, which cannot be defined by a function? Note that I do not want to supply the actual levels, as there are so many of them and I need to repeat this process many times

    – Milad
    Nov 22 '18 at 4:15











  • Well what is the order you want? If it's not the levels themselves then you will need to provide some other values that are ***actually found in the data ***

    – Elin
    Nov 22 '18 at 4:27














2












2








2


1






I have difficulties understanding the logic behind reorder().



Suppose Var is defined as below:



Var <- factor(c(0.2, 0.1, -0.1))
order(Var)


Now if I want to reorder it to c(1, 2, 3) I would run the following code, which works perfectly fine.



Needed_Order <- c(1, 2, 3)
Var <- reorder(Var, Needed_Order)
order(Var)


But it does not work if I want to reorder Var to c(3, 1, 2)



Needed_Order <- c(3,1,2)
Var <- reorder(Var, Needed_Order)
order(Var)


I expect to get 3 1 2 as the output of order(var) but it returns 2 3 1.










share|improve this question














I have difficulties understanding the logic behind reorder().



Suppose Var is defined as below:



Var <- factor(c(0.2, 0.1, -0.1))
order(Var)


Now if I want to reorder it to c(1, 2, 3) I would run the following code, which works perfectly fine.



Needed_Order <- c(1, 2, 3)
Var <- reorder(Var, Needed_Order)
order(Var)


But it does not work if I want to reorder Var to c(3, 1, 2)



Needed_Order <- c(3,1,2)
Var <- reorder(Var, Needed_Order)
order(Var)


I expect to get 3 1 2 as the output of order(var) but it returns 2 3 1.







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 3:15









MiladMilad

109110




109110








  • 1





    Why do you expect to get that? If you want to reorder the levels, you should supply the actual levels. If you read the help file for reorder (?reorder) you will see bymedian <- with(InsectSprays, reorder(spray, count, median)) which shows you that you don't supply an array of nubers, you have to supply a function that will be used for the ordering.

    – Elin
    Nov 22 '18 at 4:09











  • @Elin so what function should I use if I want a particular order, which cannot be defined by a function? Note that I do not want to supply the actual levels, as there are so many of them and I need to repeat this process many times

    – Milad
    Nov 22 '18 at 4:15











  • Well what is the order you want? If it's not the levels themselves then you will need to provide some other values that are ***actually found in the data ***

    – Elin
    Nov 22 '18 at 4:27














  • 1





    Why do you expect to get that? If you want to reorder the levels, you should supply the actual levels. If you read the help file for reorder (?reorder) you will see bymedian <- with(InsectSprays, reorder(spray, count, median)) which shows you that you don't supply an array of nubers, you have to supply a function that will be used for the ordering.

    – Elin
    Nov 22 '18 at 4:09











  • @Elin so what function should I use if I want a particular order, which cannot be defined by a function? Note that I do not want to supply the actual levels, as there are so many of them and I need to repeat this process many times

    – Milad
    Nov 22 '18 at 4:15











  • Well what is the order you want? If it's not the levels themselves then you will need to provide some other values that are ***actually found in the data ***

    – Elin
    Nov 22 '18 at 4:27








1




1





Why do you expect to get that? If you want to reorder the levels, you should supply the actual levels. If you read the help file for reorder (?reorder) you will see bymedian <- with(InsectSprays, reorder(spray, count, median)) which shows you that you don't supply an array of nubers, you have to supply a function that will be used for the ordering.

– Elin
Nov 22 '18 at 4:09





Why do you expect to get that? If you want to reorder the levels, you should supply the actual levels. If you read the help file for reorder (?reorder) you will see bymedian <- with(InsectSprays, reorder(spray, count, median)) which shows you that you don't supply an array of nubers, you have to supply a function that will be used for the ordering.

– Elin
Nov 22 '18 at 4:09













@Elin so what function should I use if I want a particular order, which cannot be defined by a function? Note that I do not want to supply the actual levels, as there are so many of them and I need to repeat this process many times

– Milad
Nov 22 '18 at 4:15





@Elin so what function should I use if I want a particular order, which cannot be defined by a function? Note that I do not want to supply the actual levels, as there are so many of them and I need to repeat this process many times

– Milad
Nov 22 '18 at 4:15













Well what is the order you want? If it's not the levels themselves then you will need to provide some other values that are ***actually found in the data ***

– Elin
Nov 22 '18 at 4:27





Well what is the order you want? If it's not the levels themselves then you will need to provide some other values that are ***actually found in the data ***

– Elin
Nov 22 '18 at 4:27












2 Answers
2






active

oldest

votes


















3














I think @prosoitos already has a great answer. I just wanted to illustrate why the reorder function exists and how it's useful.



Ordering Groups in a Plot



Let's consider the classic iris dataset



> data(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa


and suppose we want to plot the Sepal.Width values, comparing by Species



boxplot(Sepal.Width ~ Species, iris)


enter image description here



but the order here is by species name, whereas we think the plot would look nicer if we ordered by the mean sepal width of each species. And that's where reorder is a quick, powerful solution for this:



iris$Species <- reorder(iris$Species, iris$Sepal.Width, FUN=mean)
boxplot(Sepal.Width ~ Species, iris)


enter image description here



What happened here is that the iris$Sepal.Width values corresponding to each level in iris$Species had the function mean applied to them and the result was attached to the factor as the scores attribute:



> attr(iris$Species, 'scores')
setosa versicolor virginica
3.428 2.770 2.974


These scores were then used to rank (in ascending order) the levels in the factor, and assign them that order:



> levels(iris$Species)
[1] "versicolor" "virginica" "setosa"


Note that this doesn't change the order of any of the data in the data frame, but only the order of the codes used in the factor. The FUN argument makes the reorder function quite general, so that one could order by min or max or whatever function you'd like to compute on the grouped data.



Overall, I think the key thing is that the second argument in the reorder function, which was thought to be a desired order, is instead for inputting the weights or values associated to each entry in the factor.






share|improve this answer


























  • Thanks for your answer. This is in fact what I was looking for :)

    – Milad
    Nov 22 '18 at 21:49






  • 1





    And the forcats function which does that is fct_reorder()

    – prosoitos
    Nov 23 '18 at 0:59



















2














The function lvls_reorder() from the tidyverse package forcats does what you want:



Var <- factor(c(0.2, 0.1, -0.1))
Needed_Order <- c(3, 1, 2)
Var <- forcats::lvls_reorder(Var, Needed_Order)
order(Var)


Result



[1] 3 1 2




Explanations



Let's use an example with distinct elements for the values, the levels and the order positions of the levels to make it easier to visualize what is going on:



f <- factor(c(a = "A", b = "B", c = "C"))
f
# a b c
# A B C
# Levels: A B C

order(f)
# [1] 1 2 3


Now, let's use stats::reorder():



reorder(f, c(3, 1, 2))
# a b c
# A B C
# attr(,"scores")
# A B C
# 3 1 2
# Levels: B C A


reorder() assigns the values 3 1 2 as "scores" attributes for the levels A B C and reorders those levels according to these scores: reordering the scores to 1 2 3 reorders the levels to B C A.



Since order() returns a permutation which rearranges the factor into (by default) an ascending order, we get:



order(reorder(f, c(3, 1, 2)))
# [1] 2 3 1


In comparison, forcats::lvls_reorder() simply reorders the levels by indexing them with the values 3 1 2 (what you were trying to do):



lvls_reorder(f, c(3, 1, 2))
# a b c
# A B C
# Levels: C A B


Which gives the order:



order(lvls_reorder(f, c(3, 1, 2)))
# [1] 3 1 2





share|improve this answer


























  • One thing I'd suggest adding to this is how reorder actually computes the scores attribute, because that is then what is used to determine the new order for the levels attribute. It is sort of obscured in both OP and here because you both used vectors where the number of levels equals the number of entries, so you never see what the FUN=mean argument is doing.

    – merv
    Nov 22 '18 at 6:05











  • Right. I've been editing my answer a million times already and I still have a very obscure explanation. I am still working on it :P Thank you for your suggestion!

    – prosoitos
    Nov 22 '18 at 6:06













  • Note: feel free to post a new answer with a great explanation if you want. I am still battling it and I am sure that you would do this much better (I never use factors and while I know what works, it isn't yet very clear to me why things work the way they do. Which is why it is taking me so much time to explain it...)

    – prosoitos
    Nov 22 '18 at 6:09








  • 1





    Oh, it's definitely already a good, insightful answer (+1)! That reorder function is just weird though. Definitely an artifact of statisticians designing the base language.

    – merv
    Nov 22 '18 at 6:11






  • 1





    @prosoitos: Thanks for your insightful answer. This was helpful for me

    – Milad
    Nov 22 '18 at 21:48













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%2f53423360%2fthe-logic-behind-reorder-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














I think @prosoitos already has a great answer. I just wanted to illustrate why the reorder function exists and how it's useful.



Ordering Groups in a Plot



Let's consider the classic iris dataset



> data(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa


and suppose we want to plot the Sepal.Width values, comparing by Species



boxplot(Sepal.Width ~ Species, iris)


enter image description here



but the order here is by species name, whereas we think the plot would look nicer if we ordered by the mean sepal width of each species. And that's where reorder is a quick, powerful solution for this:



iris$Species <- reorder(iris$Species, iris$Sepal.Width, FUN=mean)
boxplot(Sepal.Width ~ Species, iris)


enter image description here



What happened here is that the iris$Sepal.Width values corresponding to each level in iris$Species had the function mean applied to them and the result was attached to the factor as the scores attribute:



> attr(iris$Species, 'scores')
setosa versicolor virginica
3.428 2.770 2.974


These scores were then used to rank (in ascending order) the levels in the factor, and assign them that order:



> levels(iris$Species)
[1] "versicolor" "virginica" "setosa"


Note that this doesn't change the order of any of the data in the data frame, but only the order of the codes used in the factor. The FUN argument makes the reorder function quite general, so that one could order by min or max or whatever function you'd like to compute on the grouped data.



Overall, I think the key thing is that the second argument in the reorder function, which was thought to be a desired order, is instead for inputting the weights or values associated to each entry in the factor.






share|improve this answer


























  • Thanks for your answer. This is in fact what I was looking for :)

    – Milad
    Nov 22 '18 at 21:49






  • 1





    And the forcats function which does that is fct_reorder()

    – prosoitos
    Nov 23 '18 at 0:59
















3














I think @prosoitos already has a great answer. I just wanted to illustrate why the reorder function exists and how it's useful.



Ordering Groups in a Plot



Let's consider the classic iris dataset



> data(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa


and suppose we want to plot the Sepal.Width values, comparing by Species



boxplot(Sepal.Width ~ Species, iris)


enter image description here



but the order here is by species name, whereas we think the plot would look nicer if we ordered by the mean sepal width of each species. And that's where reorder is a quick, powerful solution for this:



iris$Species <- reorder(iris$Species, iris$Sepal.Width, FUN=mean)
boxplot(Sepal.Width ~ Species, iris)


enter image description here



What happened here is that the iris$Sepal.Width values corresponding to each level in iris$Species had the function mean applied to them and the result was attached to the factor as the scores attribute:



> attr(iris$Species, 'scores')
setosa versicolor virginica
3.428 2.770 2.974


These scores were then used to rank (in ascending order) the levels in the factor, and assign them that order:



> levels(iris$Species)
[1] "versicolor" "virginica" "setosa"


Note that this doesn't change the order of any of the data in the data frame, but only the order of the codes used in the factor. The FUN argument makes the reorder function quite general, so that one could order by min or max or whatever function you'd like to compute on the grouped data.



Overall, I think the key thing is that the second argument in the reorder function, which was thought to be a desired order, is instead for inputting the weights or values associated to each entry in the factor.






share|improve this answer


























  • Thanks for your answer. This is in fact what I was looking for :)

    – Milad
    Nov 22 '18 at 21:49






  • 1





    And the forcats function which does that is fct_reorder()

    – prosoitos
    Nov 23 '18 at 0:59














3












3








3







I think @prosoitos already has a great answer. I just wanted to illustrate why the reorder function exists and how it's useful.



Ordering Groups in a Plot



Let's consider the classic iris dataset



> data(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa


and suppose we want to plot the Sepal.Width values, comparing by Species



boxplot(Sepal.Width ~ Species, iris)


enter image description here



but the order here is by species name, whereas we think the plot would look nicer if we ordered by the mean sepal width of each species. And that's where reorder is a quick, powerful solution for this:



iris$Species <- reorder(iris$Species, iris$Sepal.Width, FUN=mean)
boxplot(Sepal.Width ~ Species, iris)


enter image description here



What happened here is that the iris$Sepal.Width values corresponding to each level in iris$Species had the function mean applied to them and the result was attached to the factor as the scores attribute:



> attr(iris$Species, 'scores')
setosa versicolor virginica
3.428 2.770 2.974


These scores were then used to rank (in ascending order) the levels in the factor, and assign them that order:



> levels(iris$Species)
[1] "versicolor" "virginica" "setosa"


Note that this doesn't change the order of any of the data in the data frame, but only the order of the codes used in the factor. The FUN argument makes the reorder function quite general, so that one could order by min or max or whatever function you'd like to compute on the grouped data.



Overall, I think the key thing is that the second argument in the reorder function, which was thought to be a desired order, is instead for inputting the weights or values associated to each entry in the factor.






share|improve this answer















I think @prosoitos already has a great answer. I just wanted to illustrate why the reorder function exists and how it's useful.



Ordering Groups in a Plot



Let's consider the classic iris dataset



> data(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa


and suppose we want to plot the Sepal.Width values, comparing by Species



boxplot(Sepal.Width ~ Species, iris)


enter image description here



but the order here is by species name, whereas we think the plot would look nicer if we ordered by the mean sepal width of each species. And that's where reorder is a quick, powerful solution for this:



iris$Species <- reorder(iris$Species, iris$Sepal.Width, FUN=mean)
boxplot(Sepal.Width ~ Species, iris)


enter image description here



What happened here is that the iris$Sepal.Width values corresponding to each level in iris$Species had the function mean applied to them and the result was attached to the factor as the scores attribute:



> attr(iris$Species, 'scores')
setosa versicolor virginica
3.428 2.770 2.974


These scores were then used to rank (in ascending order) the levels in the factor, and assign them that order:



> levels(iris$Species)
[1] "versicolor" "virginica" "setosa"


Note that this doesn't change the order of any of the data in the data frame, but only the order of the codes used in the factor. The FUN argument makes the reorder function quite general, so that one could order by min or max or whatever function you'd like to compute on the grouped data.



Overall, I think the key thing is that the second argument in the reorder function, which was thought to be a desired order, is instead for inputting the weights or values associated to each entry in the factor.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 7:48

























answered Nov 22 '18 at 7:42









mervmerv

25.3k674109




25.3k674109













  • Thanks for your answer. This is in fact what I was looking for :)

    – Milad
    Nov 22 '18 at 21:49






  • 1





    And the forcats function which does that is fct_reorder()

    – prosoitos
    Nov 23 '18 at 0:59



















  • Thanks for your answer. This is in fact what I was looking for :)

    – Milad
    Nov 22 '18 at 21:49






  • 1





    And the forcats function which does that is fct_reorder()

    – prosoitos
    Nov 23 '18 at 0:59

















Thanks for your answer. This is in fact what I was looking for :)

– Milad
Nov 22 '18 at 21:49





Thanks for your answer. This is in fact what I was looking for :)

– Milad
Nov 22 '18 at 21:49




1




1





And the forcats function which does that is fct_reorder()

– prosoitos
Nov 23 '18 at 0:59





And the forcats function which does that is fct_reorder()

– prosoitos
Nov 23 '18 at 0:59













2














The function lvls_reorder() from the tidyverse package forcats does what you want:



Var <- factor(c(0.2, 0.1, -0.1))
Needed_Order <- c(3, 1, 2)
Var <- forcats::lvls_reorder(Var, Needed_Order)
order(Var)


Result



[1] 3 1 2




Explanations



Let's use an example with distinct elements for the values, the levels and the order positions of the levels to make it easier to visualize what is going on:



f <- factor(c(a = "A", b = "B", c = "C"))
f
# a b c
# A B C
# Levels: A B C

order(f)
# [1] 1 2 3


Now, let's use stats::reorder():



reorder(f, c(3, 1, 2))
# a b c
# A B C
# attr(,"scores")
# A B C
# 3 1 2
# Levels: B C A


reorder() assigns the values 3 1 2 as "scores" attributes for the levels A B C and reorders those levels according to these scores: reordering the scores to 1 2 3 reorders the levels to B C A.



Since order() returns a permutation which rearranges the factor into (by default) an ascending order, we get:



order(reorder(f, c(3, 1, 2)))
# [1] 2 3 1


In comparison, forcats::lvls_reorder() simply reorders the levels by indexing them with the values 3 1 2 (what you were trying to do):



lvls_reorder(f, c(3, 1, 2))
# a b c
# A B C
# Levels: C A B


Which gives the order:



order(lvls_reorder(f, c(3, 1, 2)))
# [1] 3 1 2





share|improve this answer


























  • One thing I'd suggest adding to this is how reorder actually computes the scores attribute, because that is then what is used to determine the new order for the levels attribute. It is sort of obscured in both OP and here because you both used vectors where the number of levels equals the number of entries, so you never see what the FUN=mean argument is doing.

    – merv
    Nov 22 '18 at 6:05











  • Right. I've been editing my answer a million times already and I still have a very obscure explanation. I am still working on it :P Thank you for your suggestion!

    – prosoitos
    Nov 22 '18 at 6:06













  • Note: feel free to post a new answer with a great explanation if you want. I am still battling it and I am sure that you would do this much better (I never use factors and while I know what works, it isn't yet very clear to me why things work the way they do. Which is why it is taking me so much time to explain it...)

    – prosoitos
    Nov 22 '18 at 6:09








  • 1





    Oh, it's definitely already a good, insightful answer (+1)! That reorder function is just weird though. Definitely an artifact of statisticians designing the base language.

    – merv
    Nov 22 '18 at 6:11






  • 1





    @prosoitos: Thanks for your insightful answer. This was helpful for me

    – Milad
    Nov 22 '18 at 21:48


















2














The function lvls_reorder() from the tidyverse package forcats does what you want:



Var <- factor(c(0.2, 0.1, -0.1))
Needed_Order <- c(3, 1, 2)
Var <- forcats::lvls_reorder(Var, Needed_Order)
order(Var)


Result



[1] 3 1 2




Explanations



Let's use an example with distinct elements for the values, the levels and the order positions of the levels to make it easier to visualize what is going on:



f <- factor(c(a = "A", b = "B", c = "C"))
f
# a b c
# A B C
# Levels: A B C

order(f)
# [1] 1 2 3


Now, let's use stats::reorder():



reorder(f, c(3, 1, 2))
# a b c
# A B C
# attr(,"scores")
# A B C
# 3 1 2
# Levels: B C A


reorder() assigns the values 3 1 2 as "scores" attributes for the levels A B C and reorders those levels according to these scores: reordering the scores to 1 2 3 reorders the levels to B C A.



Since order() returns a permutation which rearranges the factor into (by default) an ascending order, we get:



order(reorder(f, c(3, 1, 2)))
# [1] 2 3 1


In comparison, forcats::lvls_reorder() simply reorders the levels by indexing them with the values 3 1 2 (what you were trying to do):



lvls_reorder(f, c(3, 1, 2))
# a b c
# A B C
# Levels: C A B


Which gives the order:



order(lvls_reorder(f, c(3, 1, 2)))
# [1] 3 1 2





share|improve this answer


























  • One thing I'd suggest adding to this is how reorder actually computes the scores attribute, because that is then what is used to determine the new order for the levels attribute. It is sort of obscured in both OP and here because you both used vectors where the number of levels equals the number of entries, so you never see what the FUN=mean argument is doing.

    – merv
    Nov 22 '18 at 6:05











  • Right. I've been editing my answer a million times already and I still have a very obscure explanation. I am still working on it :P Thank you for your suggestion!

    – prosoitos
    Nov 22 '18 at 6:06













  • Note: feel free to post a new answer with a great explanation if you want. I am still battling it and I am sure that you would do this much better (I never use factors and while I know what works, it isn't yet very clear to me why things work the way they do. Which is why it is taking me so much time to explain it...)

    – prosoitos
    Nov 22 '18 at 6:09








  • 1





    Oh, it's definitely already a good, insightful answer (+1)! That reorder function is just weird though. Definitely an artifact of statisticians designing the base language.

    – merv
    Nov 22 '18 at 6:11






  • 1





    @prosoitos: Thanks for your insightful answer. This was helpful for me

    – Milad
    Nov 22 '18 at 21:48
















2












2








2







The function lvls_reorder() from the tidyverse package forcats does what you want:



Var <- factor(c(0.2, 0.1, -0.1))
Needed_Order <- c(3, 1, 2)
Var <- forcats::lvls_reorder(Var, Needed_Order)
order(Var)


Result



[1] 3 1 2




Explanations



Let's use an example with distinct elements for the values, the levels and the order positions of the levels to make it easier to visualize what is going on:



f <- factor(c(a = "A", b = "B", c = "C"))
f
# a b c
# A B C
# Levels: A B C

order(f)
# [1] 1 2 3


Now, let's use stats::reorder():



reorder(f, c(3, 1, 2))
# a b c
# A B C
# attr(,"scores")
# A B C
# 3 1 2
# Levels: B C A


reorder() assigns the values 3 1 2 as "scores" attributes for the levels A B C and reorders those levels according to these scores: reordering the scores to 1 2 3 reorders the levels to B C A.



Since order() returns a permutation which rearranges the factor into (by default) an ascending order, we get:



order(reorder(f, c(3, 1, 2)))
# [1] 2 3 1


In comparison, forcats::lvls_reorder() simply reorders the levels by indexing them with the values 3 1 2 (what you were trying to do):



lvls_reorder(f, c(3, 1, 2))
# a b c
# A B C
# Levels: C A B


Which gives the order:



order(lvls_reorder(f, c(3, 1, 2)))
# [1] 3 1 2





share|improve this answer















The function lvls_reorder() from the tidyverse package forcats does what you want:



Var <- factor(c(0.2, 0.1, -0.1))
Needed_Order <- c(3, 1, 2)
Var <- forcats::lvls_reorder(Var, Needed_Order)
order(Var)


Result



[1] 3 1 2




Explanations



Let's use an example with distinct elements for the values, the levels and the order positions of the levels to make it easier to visualize what is going on:



f <- factor(c(a = "A", b = "B", c = "C"))
f
# a b c
# A B C
# Levels: A B C

order(f)
# [1] 1 2 3


Now, let's use stats::reorder():



reorder(f, c(3, 1, 2))
# a b c
# A B C
# attr(,"scores")
# A B C
# 3 1 2
# Levels: B C A


reorder() assigns the values 3 1 2 as "scores" attributes for the levels A B C and reorders those levels according to these scores: reordering the scores to 1 2 3 reorders the levels to B C A.



Since order() returns a permutation which rearranges the factor into (by default) an ascending order, we get:



order(reorder(f, c(3, 1, 2)))
# [1] 2 3 1


In comparison, forcats::lvls_reorder() simply reorders the levels by indexing them with the values 3 1 2 (what you were trying to do):



lvls_reorder(f, c(3, 1, 2))
# a b c
# A B C
# Levels: C A B


Which gives the order:



order(lvls_reorder(f, c(3, 1, 2)))
# [1] 3 1 2






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 6:45

























answered Nov 22 '18 at 4:27









prosoitosprosoitos

935419




935419













  • One thing I'd suggest adding to this is how reorder actually computes the scores attribute, because that is then what is used to determine the new order for the levels attribute. It is sort of obscured in both OP and here because you both used vectors where the number of levels equals the number of entries, so you never see what the FUN=mean argument is doing.

    – merv
    Nov 22 '18 at 6:05











  • Right. I've been editing my answer a million times already and I still have a very obscure explanation. I am still working on it :P Thank you for your suggestion!

    – prosoitos
    Nov 22 '18 at 6:06













  • Note: feel free to post a new answer with a great explanation if you want. I am still battling it and I am sure that you would do this much better (I never use factors and while I know what works, it isn't yet very clear to me why things work the way they do. Which is why it is taking me so much time to explain it...)

    – prosoitos
    Nov 22 '18 at 6:09








  • 1





    Oh, it's definitely already a good, insightful answer (+1)! That reorder function is just weird though. Definitely an artifact of statisticians designing the base language.

    – merv
    Nov 22 '18 at 6:11






  • 1





    @prosoitos: Thanks for your insightful answer. This was helpful for me

    – Milad
    Nov 22 '18 at 21:48





















  • One thing I'd suggest adding to this is how reorder actually computes the scores attribute, because that is then what is used to determine the new order for the levels attribute. It is sort of obscured in both OP and here because you both used vectors where the number of levels equals the number of entries, so you never see what the FUN=mean argument is doing.

    – merv
    Nov 22 '18 at 6:05











  • Right. I've been editing my answer a million times already and I still have a very obscure explanation. I am still working on it :P Thank you for your suggestion!

    – prosoitos
    Nov 22 '18 at 6:06













  • Note: feel free to post a new answer with a great explanation if you want. I am still battling it and I am sure that you would do this much better (I never use factors and while I know what works, it isn't yet very clear to me why things work the way they do. Which is why it is taking me so much time to explain it...)

    – prosoitos
    Nov 22 '18 at 6:09








  • 1





    Oh, it's definitely already a good, insightful answer (+1)! That reorder function is just weird though. Definitely an artifact of statisticians designing the base language.

    – merv
    Nov 22 '18 at 6:11






  • 1





    @prosoitos: Thanks for your insightful answer. This was helpful for me

    – Milad
    Nov 22 '18 at 21:48



















One thing I'd suggest adding to this is how reorder actually computes the scores attribute, because that is then what is used to determine the new order for the levels attribute. It is sort of obscured in both OP and here because you both used vectors where the number of levels equals the number of entries, so you never see what the FUN=mean argument is doing.

– merv
Nov 22 '18 at 6:05





One thing I'd suggest adding to this is how reorder actually computes the scores attribute, because that is then what is used to determine the new order for the levels attribute. It is sort of obscured in both OP and here because you both used vectors where the number of levels equals the number of entries, so you never see what the FUN=mean argument is doing.

– merv
Nov 22 '18 at 6:05













Right. I've been editing my answer a million times already and I still have a very obscure explanation. I am still working on it :P Thank you for your suggestion!

– prosoitos
Nov 22 '18 at 6:06







Right. I've been editing my answer a million times already and I still have a very obscure explanation. I am still working on it :P Thank you for your suggestion!

– prosoitos
Nov 22 '18 at 6:06















Note: feel free to post a new answer with a great explanation if you want. I am still battling it and I am sure that you would do this much better (I never use factors and while I know what works, it isn't yet very clear to me why things work the way they do. Which is why it is taking me so much time to explain it...)

– prosoitos
Nov 22 '18 at 6:09







Note: feel free to post a new answer with a great explanation if you want. I am still battling it and I am sure that you would do this much better (I never use factors and while I know what works, it isn't yet very clear to me why things work the way they do. Which is why it is taking me so much time to explain it...)

– prosoitos
Nov 22 '18 at 6:09






1




1





Oh, it's definitely already a good, insightful answer (+1)! That reorder function is just weird though. Definitely an artifact of statisticians designing the base language.

– merv
Nov 22 '18 at 6:11





Oh, it's definitely already a good, insightful answer (+1)! That reorder function is just weird though. Definitely an artifact of statisticians designing the base language.

– merv
Nov 22 '18 at 6:11




1




1





@prosoitos: Thanks for your insightful answer. This was helpful for me

– Milad
Nov 22 '18 at 21:48







@prosoitos: Thanks for your insightful answer. This was helpful for me

– Milad
Nov 22 '18 at 21:48




















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%2f53423360%2fthe-logic-behind-reorder-in-r%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

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules