使用R [duplicate]按自定义顺序排列行

gpnt7bae  于 2023-01-06  发布在  其他
关注(0)|答案(4)|浏览(128)
    • 此问题在此处已有答案**:

Reorder rows using custom order(2个答案)
Arranging rows in custom order using dplyr(3个答案)
昨天关门了。
我需要根据target_cc级别顺序按国家/地区订购df。如何实现这一点?请参阅MWE

country <- rep(c("AT","BE","CY","DE","EE"),10)
value <- seq(1, 50)

target_cc <- data.frame("DE","CY","BE","AT","EE")

df <- data.frame(country, value)
df
bjp0bcyl

bjp0bcyl1#

最好的方法是将country变量设置为factor,并按照您想要的顺序排列水平,然后任何标准的排序/排序解决方案都可以对它进行处理:

# First, it's weird that target_cc is a data frame with these columns
# I'm hoping that was a typo in your question, and we can use it as a
# vector instead. If not, we can create the vector from the data frame
# with unlist():

target_cc
#  X.DE. X.CY. X.BE. X.AT. X.EE.
#1    DE    CY    BE    AT    EE

# useless as data frame, useful as vector
target_cc_v = unlist(target_cc)
# or fix the definition
target_cc_v = c("DE","CY","BE","AT","EE")

# Make country a factor with the levels in this order:
df$country = factor(df$country, levels = target_cc_v)

# Any standard sort/order solution should now work
df[order(df$country, df$value), ]
#    country value
# 4       DE     4
# 9       DE     9
# 14      DE    14
# 19      DE    19
# 24      DE    24
# 29      DE    29
# 34      DE    34
# 39      DE    39
# 44      DE    44
# 49      DE    49
# 3       CY     3
# 8       CY     8
# ...
wn9m85ua

wn9m85ua2#

您可以将country设为有序因子。

library(dplyr)

country <- rep(c("AT","BE","CY","DE","EE"),10)
value <- seq(1, 50)

# chenged this to a vector rather than a data frame
target_cc <- c("DE","CY","BE","AT","EE")

df %>% 
  mutate(country = factor(country, levels = target_cc)) %>% 
  arrange(country)
ct3nt3jp

ct3nt3jp3#

我不确定我是否正确理解了您的要求,所以如果这不是您要找的,请告诉我。
您可以使用target_cc与df连接,但它需要与df的长度相同。
使用dplyr

library(dplyr)

country <- rep(c("AT","BE","CY","DE","EE"), 10)

value <- seq(1, 50)

df <- data.frame(country, value)

target <- data.frame(
  country = rep(c("DE","CY","BE","AT","EE"), times = 5)
)

df2 <- df %>%
  right_join(target, by = "country") %>%
  distinct()

head(df2)
#>   country value
#> 1      DE     4
#> 2      DE     9
#> 3      DE    14
#> 4      DE    19
#> 5      DE    24
#> 6      DE    29

tail(df2)
#>    country value
#> 45      EE    25
#> 46      EE    30
#> 47      EE    35
#> 48      EE    40
#> 49      EE    45
#> 50      EE    50

reprex package(v0.3.0)于2020年2月7日创建
由于这将产生叉积,因此使用distinct只保留唯一的行。

yizd12fk

yizd12fk4#

使用order的碱R溶液

dfout <- df[order(match(df$country,unlist(target_cc))),]

它给出了

> dfout
   country value
4       DE     4
9       DE     9
14      DE    14
19      DE    19
24      DE    24
29      DE    29
34      DE    34
39      DE    39
44      DE    44
49      DE    49
3       CY     3
8       CY     8
13      CY    13
18      CY    18
23      CY    23
28      CY    28
33      CY    33
38      CY    38
43      CY    43
48      CY    48
2       BE     2
7       BE     7
12      BE    12
17      BE    17
22      BE    22
27      BE    27
32      BE    32
37      BE    37
42      BE    42
47      BE    47
1       AT     1
6       AT     6
11      AT    11
16      AT    16
21      AT    21
26      AT    26
31      AT    31
36      AT    36
41      AT    41
46      AT    46
5       EE     5
10      EE    10
15      EE    15
20      EE    20
25      EE    25
30      EE    30
35      EE    35
40      EE    40
45      EE    45
50      EE    50

相关问题