Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dplyr按字符串对行重新排序_R_Dplyr - Fatal编程技术网

dplyr按字符串对行重新排序

dplyr按字符串对行重新排序,r,dplyr,R,Dplyr,我有以下数据: library(tidyverse) d1 <- data_frame(Nat = c("UK", "UK", "UK", "NONUK", "NONUK", "NONUK"), Type = c("a", "b", "c", "a", "b", "c")) 库(tidyverse) d1您可以使用match string_order <- c("b", "c", "a") d1 %>% group_by(Nat) %>% mutate

我有以下数据:

library(tidyverse)


d1 <- data_frame(Nat = c("UK", "UK", "UK", "NONUK", "NONUK", "NONUK"),
 Type = c("a", "b", "c", "a", "b", "c"))
库(tidyverse)

d1您可以使用
match

string_order <- c("b", "c", "a")
d1 %>% 
  group_by(Nat) %>% 
  mutate(Type = Type[match(string_order, Type)]) %>%
  ungroup()
#  A tibble: 6 x 2
#  Nat   Type 
#  <chr> <chr>
#1 UK    b    
#2 UK    c    
#3 UK    a    
#4 NONUK b    
#5 NONUK c    
#6 NONUK a 
string\u顺序%
分组依据(Nat)%>%
变异(类型=类型[匹配(字符串顺序,类型)])%>%
解组()
#一个tibble:6x2
#Nat型
#   
#1英国b
#2英国c
#3英国a
#4非英国b
#5非英国c
#6非英国a

您可以使用
匹配

string_order <- c("b", "c", "a")
d1 %>% 
  group_by(Nat) %>% 
  mutate(Type = Type[match(string_order, Type)]) %>%
  ungroup()
#  A tibble: 6 x 2
#  Nat   Type 
#  <chr> <chr>
#1 UK    b    
#2 UK    c    
#3 UK    a    
#4 NONUK b    
#5 NONUK c    
#6 NONUK a 
string\u顺序%
分组依据(Nat)%>%
变异(类型=类型[匹配(字符串顺序,类型)])%>%
解组()
#一个tibble:6x2
#Nat型
#   
#1英国b
#2英国c
#3英国a
#4非英国b
#5非英国c
#6非英国a

如何明确dplyr链中的级别,以选择您的订单:

library(dplyr)
d1 %>%
arrange(factor(.$Nat, levels = c("UK", "NONUK")), factor(.$Type, levels = c("c", "b","a")))

# A tibble: 6 x 2
  Nat   Type 
  <chr> <chr>
1 UK    c    
2 UK    b    
3 UK    a    
4 NONUK c    
5 NONUK b    
6 NONUK a
库(dplyr)
d1%>%
排列(系数(.$Nat,等级=c(“英国”,“非英国”)),系数(.$Type,等级=c(“c”,“b”,“a”))
#一个tibble:6x2
Nat型
1英国c
2英国b
3英国a
4非英国c
5非英国b
6非英国a
另一个例子:

d1 %>%
arrange(factor(.$Nat, levels = c("UK", "NONUK")), factor(.$Type, levels = c("b", "c","a")))
# A tibble: 6 x 2
  Nat   Type 
  <chr> <chr>
1 UK    b    
2 UK    c    
3 UK    a    
4 NONUK b    
5 NONUK c    
6 NONUK a 
d1%>%
排列(系数(.$Nat,等级=c(“英国”、“非英国”)),系数(.$Type,等级=c(“b”、“c”、“a”))
#一个tibble:6x2
Nat型
1英国b
2英国c
3英国a
4非英国b
5非英国c
6非英国a

如何明确dplyr链中的级别,以选择您的订单:

library(dplyr)
d1 %>%
arrange(factor(.$Nat, levels = c("UK", "NONUK")), factor(.$Type, levels = c("c", "b","a")))

# A tibble: 6 x 2
  Nat   Type 
  <chr> <chr>
1 UK    c    
2 UK    b    
3 UK    a    
4 NONUK c    
5 NONUK b    
6 NONUK a
库(dplyr)
d1%>%
排列(系数(.$Nat,等级=c(“英国”,“非英国”)),系数(.$Type,等级=c(“c”,“b”,“a”))
#一个tibble:6x2
Nat型
1英国c
2英国b
3英国a
4非英国c
5非英国b
6非英国a
另一个例子:

d1 %>%
arrange(factor(.$Nat, levels = c("UK", "NONUK")), factor(.$Type, levels = c("b", "c","a")))
# A tibble: 6 x 2
  Nat   Type 
  <chr> <chr>
1 UK    b    
2 UK    c    
3 UK    a    
4 NONUK b    
5 NONUK c    
6 NONUK a 
d1%>%
排列(系数(.$Nat,等级=c(“英国”、“非英国”)),系数(.$Type,等级=c(“b”、“c”、“a”))
#一个tibble:6x2
Nat型
1英国b
2英国c
3英国a
4非英国b
5非英国c
6非英国a

阅读有关<代码>?系数
。阅读有关<代码>?系数。谢谢。如果我在d1中添加另一个变量:
number(c(1,2,3,4,5,6))
?如何确保此变量遵循我指定的顺序。希望这是有道理的。如果没有意义,我可以编辑这个问题(如果这是最佳做法,我可以写一个新的问题)。你是说列
类型的顺序应该改变,而列
编号保持
c(1,2,3,4,5,6)
?那么上面的代码应该可以工作了。如果没有,最好用示例数据和预期输出提出一个新问题。BestNo我希望重新排列
编号
变量,以匹配
类型
的顺序。因此,它将变成
2,3,1,5,6,4
,我猜您需要列
Nat
Type
作为您需要在s_t的答案中指定级别的因素。让这成为一个新问题,我们将看看是否有更通用的dplyr方法。好的,在这里问了一个新问题:谢谢。如果我在d1中添加另一个变量:
number(c(1,2,3,4,5,6))
?如何确保此变量遵循我指定的顺序。希望这是有道理的。如果没有意义,我可以编辑这个问题(如果这是最佳做法,我可以写一个新的问题)。你是说列
类型的顺序应该改变,而列
编号保持
c(1,2,3,4,5,6)
?那么上面的代码应该可以工作了。如果没有,最好用示例数据和预期输出提出一个新问题。BestNo我希望重新排列
编号
变量,以匹配
类型
的顺序。因此,它将变成
2,3,1,5,6,4
,我猜您需要列
Nat
Type
作为您需要在s_t的答案中指定级别的因素。让这成为一个新问题,我们将看看是否有更通用的dplyr方法。好的,在这里问了一个新问题: