Sorting a column based on the order of another column in R

无人久伴 提交于 2019-12-08 07:50:57

问题


The R script below creates a data frame a123 with three columns. Column a1 has three variables occurring at different places with corresponding a2 and a3 values.

a1 = c("A", "B", "C", "A", "B", "B", "A", "C", "A", "C", "B")
a2 = c( 10, 8, 11 , 6 , 4 , 7 , 9 , 1 , 3 , 2, 7)
a3 = c( 55, 34, 33, 23, 78, 33, 123, 34, 85, 76, 74)
a123 = data.frame(a1, a2, a3)

My need is that I want a3 column values corresponding to a1 column values to be arranged in ascending order based on the order of a2 values. Also, if common a2 values are encountered, the corresponding a3 column values should be arranged in ascending order. For example, say value "A" in column a1 has following values in a2 and a3,

a2 = c(10, 6, 9, 3)
a3 = c(55, 23, 123, 85)

The values can be like:

a3 = c(123, 23, 85, 55)

Expected Outcome:

a1 = c("A", "B", "C", "A", "B", "B", "A", "C", "A", "C", "B")
a2 = c( 10, 8, 11, 6, 4, 7, 9, 1, 3, 2, 7)
a3 = c( 123, 78, 76, 23, 33, 34, 85, 33, 55, 34, 74)
a123 = data.frame(a1, a2, a3)

Thanks and please help. Note: Please try to avoid loops and conditions as they might slow the computation based on large data.


回答1:


A solution using dplyr, sort, and rank. I do not fully understand your logic, but this is probably something you are looking for. Notice that I assume the elements in a3 of group A is 123, 55, 85, 23.

library(dplyr)

a123_r <- a123 %>%
  group_by(a1) %>%
  mutate(a3 = sort(a3, decreasing = TRUE)[rank(-a2, ties.method = "last")]) %>%
  ungroup() %>%
  as.data.frame()
a123_r
#    a1 a2  a3
# 1   A 10 123
# 2   B  8  78
# 3   C 11  76
# 4   A  6  55
# 5   B  4  33
# 6   B  7  34
# 7   A  9  85
# 8   C  1  33
# 9   A  3  23
# 10  C  2  34
# 11  B  7  74


来源:https://stackoverflow.com/questions/48037056/sorting-a-column-based-on-the-order-of-another-column-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!