将具有最大值的列的索引添加为新列 [英] Add the index of the column with the maximum value as a new column

查看:28
本文介绍了将具有最大值的列的索引添加为新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单.当数据如下时,

My question is simple. When data is as below,

var1    var2    var3
10      40      60
15      10      5

我想添加一个新列 MaxValueVar,它返回在 var1var2 中具有最大值的列的索引var3.也就是说,我想制作一个如下所示的表格.

I want to add a new column MaxValueVar that returns index of a column that has maximum value among var1, var2 and var3. That is, I want to make a table as below.

var1    var2    var3    MaxValueVar
10      40      60      3
15      10      5       1

在 R 中我会使用:

apply(vector, 1, which.max)

如何使用 SAS 完成此操作?

How can I accomplish this using SAS?

推荐答案

根据您在此处提供的示例,一个解决方案供您参考.你没有提到如何处理关系.在这里,对于平局,获取第一次出现.

One Solution for your reference according to the sample you provide here. You didn't mention how to deal with ties. Here for ties, the first occurrence is fetched.

data test;
input var1 var2 var3;
datalines;
10      40      60
15      10      5
run;

data test;
 set test;
 maxvalue=max(of var1-var3);
 maxvaluevar=0;
  array vars (*) var1-var3;
    do i=1 to dim(vars);
     if maxvaluevar=0 then maxvaluevar=i*(maxvalue=vars(i))+maxvaluevar;
    end;
 drop i maxvalue;
run;

这篇关于将具有最大值的列的索引添加为新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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