简体   繁体   English

查找每行最大值的列索引

[英]Find column indices for max values of each row

I'm new to programming in R and I have the following dataframe:我是 R 编程新手,我有以下 dataframe:

 A B C D E
1 3 0 4 5 0
2 0 0 5 1 0
3 2 1 2 0 3

I would like to get a new dataframe containing the indices of the n max values of each row, eg: If I wanted the column indices of the 3 biggest values in each row (n=3), I want my new dataframe to be like this:我想获得一个新的 dataframe 包含每行的 n 个最大值的索引,例如:如果我想要每行中 3 个最大值的列索引(n = 3),我希望我的新 dataframe 像这个:

  F G H
1 1 3 4
2 1 3 4
3 1 3 5

So in the first row of this dataframe containts the column indices of the 3 biggest values of row 1 in the original dataframe.所以在这个 dataframe 的第一行包含原始 dataframe 中第 1 行的 3 个最大值的列索引。 And so on.等等。

My original idea was to write a loop with which.max, but that seems way too long and ineffective.我最初的想法是用 which.max 编写一个循环,但这似乎太长且无效。 Does anyone have a better idea?有没有人有更好的主意?

We can use apply我们可以使用apply

t(apply(df1, 1, function(x) sort(head(seq_along(x)[order(-x)], 3))))
#   [,1] [,2] [,3]
#1    1    3    4
#2    1    3    4
#3    1    3    5

Or using tidyverse或使用tidyverse

library(dplyr)
library(tidyr)
df1 %>%
   mutate(rn = row_number()) %>% 
   pivot_longer(cols = -rn) %>% 
   group_by(rn) %>% 
   mutate(ind = row_number()) %>% 
   arrange(rn, desc(value)) %>% 
   slice(n = 1:3)  %>% 
   select(-name, -value) %>% 
   arrange(rn, ind) %>%
   mutate(nm1 = c("F", "G", "H")) %>% 
   ungroup %>% 
   pivot_wider(names_from = nm1, values_from = ind)

data数据

df1 <- structure(list(A = c(3L, 0L, 2L), B = c(0L, 0L, 1L), C = c(4L, 
5L, 2L), D = c(5L, 1L, 0L), E = c(0L, 0L, 3L)), class = "data.frame",
row.names = c("1", 
"2", "3"))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM