R中多列的每行排名 [英] Rank per row over multiple columns in R

查看:40
本文介绍了R中多列的每行排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R 来分析我的硕士论文.不幸的是,我遇到了这个问题:

I'm using R for the analysis of my masterthesis. Unfortunately, I got stuck with this problem:

我想计算一个新变量,它计算多个变量中每行一个变量的排名.

I would like to compute a new variable which calculates the rank of one variable per row within many variables.

示例:

V1    V2    V3   NewVariable_V1 NewVariable_V2 NewVariable_V3
11    21    35   3              2               1
22    12    66   2              3               1
44    22    12   1              2               3

推荐答案

您正在寻找排名.要获得降序,请先否定 data.frame.

You're looking for rank. To get decreasing order, first negate the data.frame.

data.frame(d, t(apply(-d, 1, rank, ties.method='min')))
#   V1 V2 V3 V1.1 V2.1 V3.1
# 1 11 21 35    3    2    1
# 2 22 12 66    2    3    1
# 3 44 22 12    1    2    3

这篇关于R中多列的每行排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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