R:如何在每行中生成最高值的向量? [英] R: How to generate vectors of highest value in each row?

查看:76
本文介绍了R:如何在每行中生成最高值的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的数据框包含

 > DF 
V1 V2 V3
1 0.3 0.4 0.7
2 0.4 0.2 0.1
3 0.2 0.8 0.3
4 0.5 0.8 0.9
5 0.2 0.7 0.8
6 0.8 0.3 0.6
7 0.1 0.5 0.4

行将是不同的类型的汽车,
,列将是给定类别的V1,V2,V3的概率。



我想生成一个矢量,分配给每个汽车它的概率最高的类别
例如,我想要汽车1与V3相关联,汽车2与V1相关联,汽车3与V2关联。



任何帮助或提示如何解决这个问题?

解决方案

我们可以使用 max.col 获取与每行最大值相对应的列索引。

 名称(DF)[max.col(DF,first)] 
#[1]V3V1V2V3V3V1


Let's say that my data frame contains

> DF
   V1   V2   V3  
1  0.3  0.4  0.7  
2  0.4  0.2  0.1  
3  0.2  0.8  0.3  
4  0.5  0.8  0.9   
5  0.2  0.7  0.8  
6  0.8  0.3  0.6  
7  0.1  0.5  0.4  

the rows would be the different types of automobiles, and the columns would be the probabilities for a given category of V1, V2, V3.

I want to generate a vector that assigns to each automobile the category of which it has the highest probability in. For example, I'd want automobile 1 to be associated with V3, automobile 2 to be associated with V1, automobile 3 to be associated with V2.

Any aids or hints on how I should tackle this?

解决方案

We can use max.col to get the column index that corresponds to the largest value in each row.

names(DF)[max.col(DF, "first")]
#[1] "V3" "V1" "V2" "V3" "V3" "V1" "V2"

这篇关于R:如何在每行中生成最高值的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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