简体   繁体   English

如何使用 dplyr 查找列在一行中的排名?

[英]How to find where a column ranks across a row using dplyr?

Say I have a dataframe that looks like this:假设我有一个如下所示的数据框:

dat <- data.frame(
  Iowa = c(11, 12, 15),
  Wisconsin = c(10, 14, 12),
  Florida = c(14, 9, 11)
)

I want to get the rowwise rank for Iowa relative to the other columns.我想获得爱荷华州相对于其他列的行列排名。 So the output would look like this:所以输出看起来像这样:

out <- data.frame(
  Iowa = c(11, 12, 15),
  Wisconsin = c(10, 14, 12),
  Florida = c(14, 9, 11),
  IowaRank = c(2, 2, 1)
)

What's the best way to achieve this using R, ideally in a dplyr pipe?使用 R 实现这一目标的最佳方法是什么,最好是在dplyr管道中?

Easiest will be apply to loop over the rows, apply the rank extract the first row最简单的将apply循环行,应用rank提取第一行

dat$IowaRank <- apply(-dat, 1, rank)[1,]

-output -输出

dat
  Iowa Wisconsin Florida IowaRank
1   11        10      14        2
2   12        14       9        2
3   15        12      11        1

Or using rowRanks from matrixStats或使用rowRanksmatrixStats

library(matrixStats)
dat$IowaRank <- rowRanks(-as.matrix(dat))[,1]

Or with dplyr或者用dplyr

library(dplyr)
dat %>%
    rowwise %>%
    mutate(IowaRank = rank(-c_across(everything()))[1]) %>%
    ungroup
# A tibble: 3 x 4
   Iowa Wisconsin Florida IowaRank
  <dbl>     <dbl>   <dbl>    <dbl>
1    11        10      14        2
2    12        14       9        2
3    15        12      11        1

You can get the ranks for all the columns using dense_rank -您可以使用dense_rank获取所有列的dense_rank -

library(dplyr)
library(tidyr)

dat %>%
  mutate(row = row_number()) %>%
  pivot_longer(cols = -row) %>%
  group_by(row) %>%
  mutate(rank = dense_rank(-value)) %>%
  ungroup %>%
  pivot_wider(names_from = name, values_from = c(value, rank)) %>%
  select(-row)

#  value_Iowa value_Wisconsin value_Florida rank_Iowa rank_Wisconsin rank_Florida
#       <dbl>           <dbl>         <dbl>     <int>          <int>        <int>
#1         11              10            14         2              3            1
#2         12              14             9         2              1            3
#3         15              12            11         1              2            3

If you are interested only in rank of Iowa using rowwise you can do -如果您只对使用rowwise的爱荷华州排名感兴趣,您可以这样做 -

dat %>%
  rowwise() %>%
  mutate(IowaRank = dense_rank(-c_across())[1])

#   Iowa Wisconsin Florida IowaRank
#  <dbl>     <dbl>   <dbl>    <int>
#1    11        10      14        2
#2    12        14       9        2
#3    15        12      11        1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 dplyr 中的列名向量在列中查找行最大值 - Find row maximum across columns by using vector of column names in dplyr Dplyr:对 R 中的多个整列使用 mutate、cross、where 和 ìfelse - Dplyr: using mutate , across , where and ìfelse to multiple entire column in R 使用 dplyr 中的列号 - Using column numbers in dplyr across Glue 列规范中的“fn”和“col”arguments 在哪里命名 dplyr 中的列(使用 `across` 和 `where` 来自哪里? - Where do the “fn” and “col” arguments to the Glue column specifications to name columns in dplyr (using `across` and `where` come from? R dplyr purrr 查找跨多个列的列最小值的索引值和索引处的相应行值 - R dplyr purrr find index value of column minimum across multiple columns and corresponding row value at index 如何使用 dplyr 找到每一行具有特定值的第一列 - How to find the first column with a certain value for each row with dplyr 跨越使用dplyr后的列名 - Column names after using dplyr across 使用 dplyr 为一组列创建降序 - Create descending ranks for a set of columns using dplyr 如何在R中使用dplyr跨列减去具有奇数行号的偶数行号 - How to subtract even row numbers with odd row numbers across columns using dplyr in R 如何使用dplyr和stringr替换特定列中的每一行的字符串 - How to replace string for every row in specfic column using dplyr and stringr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM