[英]How can I create rank variables for each other variables in R?
Hello dear community members.亲爱的社区成员,您好。 I'm trying to create ranking variables for certain variables in R. For example I want to transform this data frame
我正在尝试为 R 中的某些变量创建排名变量。例如,我想转换此数据框
> df
X1 X2 X3 X4 X5
1 1 4 7 3 2
2 2 5 8 4 3
3 3 6 3 5 4
4 4 1 2 6 5
5 5 2 1 7 6
into进入
> df
X1 X2 X3 X4 X5 x1_rank x2_rank x3_rank
1 1 4 7 3 2 3 2 1
2 2 5 8 4 3 3 2 1
3 3 6 3 5 4 3 1 3
4 4 1 2 6 5 1 3 2
5 5 2 1 7 6 1 2 3
like this (select X1~X3, and make ranking variables between them).像这样(选择X1~X3,在它们之间做排序变量)。
I tried this code我试过这段代码
for (i in 1:nrow(df)) {
df_rank <- df[i, ] %>%
dplyr::select(X1, X2, X3, X4) %>%
base::rank()
}
I can imagine I can solve this problem by using for loop but I'm beginner about R so I do not understand why this doesn't work.我可以想象我可以通过使用 for 循环来解决这个问题,但我是 R 的初学者,所以我不明白为什么这不起作用。
One way to achieve it is to use the ties argument on negative values.实现它的一种方法是对负值使用 ties 参数。
df <- tibble::tribble(
~x1, ~x2, ~x3, ~x4, ~x5,
1,4,7,3,2,
2,5,8,4,3,
3,6,3,5,4,
4,1,2,6,5,
5,2,1,7,6
)
library(magrittr)
df %>%
cbind(
t(apply(-df[,1:3], 1, rank, ties = "min")) %>% {colnames(.) <- paste0(colnames(.), "_rank"); .}
)
x1 x2 x3 x4 x5 x1_rank x2_rank x3_rank
1 1 4 7 3 2 3 2 1
2 2 5 8 4 3 3 2 1
3 3 6 3 5 4 2 1 2
4 4 1 2 6 5 1 3 2
5 5 2 1 7 6 1 2 3
As to why your code does not work - the for loop does not return anything, instead, it assigns a variable df_rank
every iteration.至于为什么您的代码不起作用 - for 循环不返回任何内容,而是在每次迭代时分配一个变量
df_rank
。 To fix it, you could declare an object outside of the loop, and add content to it each iteration, and finally bind that to the original data.要修复它,您可以在循环外声明一个 object,并在每次迭代中向其添加内容,最后将其绑定到原始数据。
m <- matrix(ncol = 3, nrow = 5)
for (i in 1:nrow(df)) {
m[i,] <- -df[i, ] %>%
dplyr::select(x1, x2, x3) %>%
base::rank(ties = "min")
}
colnames(m) <- paste0(names(df)[1:3], "_rank")
df %>% bind_cols(m)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.