[英]Sort a dataframe based on a specific column in R with dplyr
I have a dataframe that looks like this我有一个看起来像这样的数据框
df <- data.frame(time=seq(1,4,1),col1=c("a","b","d","c"), col2=c("d","a","c","b"))
df
#> time col1 col2
#> 1 1 a d
#> 2 2 b a
#> 3 3 d c
#> 4 4 c b
Created on 2021-11-06 by the reprex package (v2.0.1)由reprex 包(v2.0.1) 于 2021 年 11 月 6 日创建
I want to sort my data frame based on col2 and look like this我想根据 col2 对我的数据框进行排序,看起来像这样
time col1 col2
3 d d
1 a a
4 c c
2 b b
Any ideas or help is highly appreciated!任何想法或帮助都非常感谢!
Don't know, if this makes any sense, but you could do a self join:不知道,如果这有任何意义,但您可以进行自连接:
library(tidyr)
library(dplyr)
df %>%
select(col2) %>%
inner_join(df %>% mutate(col2 = col1), by = "col2") %>%
select(time, col1, col2)
This returns这返回
time col1 col2
1 3 d d
2 1 a a
3 4 c c
4 2 b b
A solution in base R:基础 R 中的解决方案:
df <- data.frame(time = match(df$col2,df$col1), col1 = df$col2, col2=df$col2)
#> time col1 col2
#> 1 3 d d
#> 2 1 a a
#> 3 4 c c
#> 4 2 b b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.