简体   繁体   English

使用 dplyr 根据 R 中的特定列对数据框进行排序

[英]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.

相关问题 对R中的数据框进行排序(基于列值) - Sort dataframe in R (based on column values) R 使用 dplyr 根据列中的最大值变异 dataframe - R mutate a dataframe based on max in a column using dplyr 在 R 和 dplyr 中排序:如何根据另一列中的类别总和按类别排序? - Sorting in R with dplyr: How to sort by category in one column based on sum of category in another column? R dplyr 列用字母数字字符排序 - R dplyr column sort with alphanumeric characters 使用 dplyr 将新列添加到 R 中的数据框 - Add new column to dataframe in R using dplyr R dplyr-根据特定值在另一列中的位置从一列中选择值 - R dplyr - select values from one column based on position of a specific value in another column 在使用 dplyr 的特定列值的第一个实例之后过滤 dataframe 中的 R 中的行 - Filter rows in dataframe in R after first instance of specific column value using dplyr 如何通过根据另一个数据帧的行名的顺序映射一个数据帧的列名来对 R 中的数据帧进行排序? - How to sort a dataframe in R by mapping column names of one dataframe based on the order of row names of another dataframe? 在SparkR中对DataFrame中的特定列进行排序 - To sort a specific column in a DataFrame in SparkR R dplyr:将列更改为特定行范围 - R dplyr: mutate a column for specific row range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM