简体   繁体   English

查找与R中每一行的最小值对应的列名

[英]find column names corresponding to least value of each row in R

i have to extract column names corresponding to least 10 values for each row of a data frame and print it in new data frame.Can anyone help me do that? 我必须为数据框的每一行提取至少与10个值相对应的列名,并将其打印在新的数据框中。有人可以帮我吗?

my data frame looks like this:(15 x 15 matrix) 我的数据框看起来像这样:(15 x 15矩阵)

    > head(yt)
        V2   V3   V4   V5   V6   V7   V8   V9  V10  V11  V12  V13  V14  V15 
    1    0  966 1513 2964 1149  927 1611 1510  390  466 1820 1718  504 1179  
    2  966    0 2410 1520 1817  729  686  290 1823  168 2027  172 1610 1500 
    3 1513 2410    0  604  481 2742 1833  826  214 1618 1712  567  544 1313 
    4 2964 1520  604    0  595 1289 1446  466 1139  430  428  201  452 1574 
    5 1149 1817  481  595    0  494  550 2641  765  934 1813 1516  378 1363 
    6  927  729 2742 1289  494    0 1279 1197 2956  299  888  491 1408 1432

for each of the 15 rows i would like to know column names for least 10 columns 对于15行中的每一行,我想知道至少10列的列名

i know which.min gives least column name for each row but can you let me know other ways of computing least 10 values 我知道which.min为每一行提供最少的列名,但是您能让我知道其他计算最少10个值的方法吗?

The order function will give the ordering of a vector. order函数将给出向量的order数。 So the first value of its result corresponds to the index of the smallest value in the vector. 因此,其结果的第一个值对应于向量中最小值的索引。 The second to the index of the second smallest etc. Using it in a function and using the apply function we can apply it to all rows. 第二个到第二个最小的索引等。在函数中使用它并使用apply函数,我们可以将其应用于所有行。 The apply function's result is column wise, which you probably don't want, so the transpose is taken over the result. apply函数的结果是按列排列的,您可能不希望这样做,因此转置将接管结果。

first_10 <- function(x) order(x)[1:10]
t(apply(dat, 1, first_10))

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

相关问题 在r的数据框中找到每行的第一,第二和第三最大值及其对应的列名 - Find first, second, and third maximum of each row and their corresponding column names in a data frame in r 在列名称中搜索字符串,并为每一行检索相应的值 - search for string in column names and retrieve corresponding value for each row R:对应于每一行最大值的索引 - R: Index corresponding to maximum value in each row 找到R中每行最接近第一列中的值的值 - Find the value nearest to the value in the first column for each row in R R dplyr purrr 查找跨多个列的列最小值的索引值和索引处的相应行值 - R dplyr purrr find index value of column minimum across multiple columns and corresponding row value at index 如何在 R 中找到最后一列的值(每行)? - How to find last column with value (for each row) in R? 对于每一行,按最高值对列名称进行排序 - For each row, sort column names by highest value 获取每行的最大值列的名称 - Get names of column with max value for each row 如何在不混淆R中顺序的情况下将一列的每个值添加到另一列的对应行下 - How to add each value of one column beneath its corresponding row of another column without mixing up the order in R 找出列的“max”和相应行的另一列的值 - Find out the “max” of a column and the value of another column for the corresponding row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM