如何通过逐渐增加数据的顺序组合来突变新列? [英] How to mutate new columns with gradually increasing sequential combinations of data?

查看:59
本文介绍了如何通过逐渐增加数据的顺序组合来突变新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

df的示例:

df <- tibble(name = LETTERS[1:10],
              x = rnorm(10, mean = 10), 
              y = rnorm(10, 10), 
              z = rnorm(10, 10))

我想对x的排名列进行突变,然后对cols xy的总和进行变异,然后对xyz进行变异,其中较大的数字排名为1,然后最小的数字10.

I would like to mutate ranked columns for x, then the sums of cols x and y, then x and y and z, where the bigger numbers are ranked 1, then the smallest numbers 10.

x开始,我可以做类似的事情:

Starting with x, I could do something like:

df %<>% mutate(rank_01 = min_rank(-x))

哪个计算x的排名列,但是我不确定计算后面的列的最佳过程是什么.我想以某种方式利用向量化的优势,但是我的编程技能在这里受到限制.

Which computes the ranked column for x, but then I'm not sure what the best process would be to compute the latter columns. I'm guessing taking advantage of vectorisation somehow, but my programming skills are limited here.

在我的真实df中,我要使用的cols总数大于50,因此自动处理是理想的选择!

In my real df, the total number of cols I would like to do this with is >50, so an automated process is ideal!

预期输出:

# A tibble: 10 x 7
   name      x rank_01     y rank_02     z rank_03
 * <chr> <dbl>   <dbl> <dbl>   <dbl> <dbl>   <dbl>
 1 A      9.37       8 11.5        4 10.9        2
 2 B     10.2        6 10.4        5 10.8        3
 3 C      9.16      10  9.38      10 10.1        9
 4 D     11.6        1  7.79       8  8.01      10
 5 E     10.3        5 11.1        2 10.6        1
 6 F      9.18       9  9.96       9  9.94       8
 7 G     10.5        4  9.98       6  9.84       6
 8 H     10.7        2 10.9        1  8.53       7
 9 I     10.6        3 10.8        3  9.52       4
10 J      9.69       7 10.6        7 10.4        5

推荐答案

cbind(df, apply(-apply(df[, -1], 1, cumsum), 1, rank))
#    name         x         y         z  x  y  z
# 1     A 10.049312 10.424365  9.286644  5  4  5
# 2     B 10.010068 10.996667  8.754025  6  1  4
# 3     C  9.813097  9.493180 10.651993  9  7  3
# 4     D 10.702742  9.657496  9.838946  3  5  2
# 5     E  9.936206  9.047051  8.938002  7 10 10
# 6     F  9.833105  9.205973 10.627177  8  9  6
# 7     G 11.310733  9.262942  8.931759  2  3  7
# 8     H 11.316306  8.576866 12.390953  1  6  1
# 9     I  9.044812 10.251189  9.606649 10  8  9
# 10    J 10.495743 10.174724  8.458670  4  2  8

您可能还想将列名设置为rank_x_rank_xy等.请参见累计粘贴(连接)的值由另一个变量分组.例如,

You may also want to set the column names to something like rank_x, _rank_xy, etc. See Cumulatively paste (concatenate) values grouped by another variable for that. E.g.,

paste0("rank_", Reduce(paste0, names(df)[-1], accumulate = TRUE))
# [1] "rank_x"   "rank_xy"  "rank_xyz"

这篇关于如何通过逐渐增加数据的顺序组合来突变新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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