如何将列名映射到行最小值 [英] how to map column names to row minimum value

查看:58
本文介绍了如何将列名映射到行最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

df
     dc1  dc2  dc3   min_colname
[1,] 12.9 13.4 13.4
[2,]  6.1  6.5  6.5
[3,]  6.3  6.7  6.7
[4,] 21.0 21.4 21.4
[5,]  1.6  1.8  1.8
[6,]  3.3  3.7  3.7
[7,]  7.0  7.4  7.4
[8,]  3.2  3.6  3.6
[9,] 14.8 15.2 15.2
[10,]  7.9  8.3  8.3

我正尝试再添加一个列,例如 min_colname ,该列将具有每行的最小值,但映射到列名...例如,第1行的最小值是 12.9 .. so,因此min_colname中的第一项应为 dc1 ,而不是实际的obs值.....

I am trying to add one more column say min_colname which will have the min values of each row but mapped to column name....for example for row 1 min value is 12.9..so the first item in min_colname should be dc1 and not the actual obs value.....

推荐答案

这应该有效:

df$min_colname  <- apply(df, 1, function(x) colnames(df)[which.min(x)])

这篇关于如何将列名映射到行最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆