简体   繁体   English

如何为 R 中的其他变量创建等级变量?

[英]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.

相关问题 如何在 R 中创建许多新变量,每个变量都基于多个其他变量? - How do I create many new variables in R, each of them based on multiple other variables? 如何将所有变量相互交叉并在 R 中收集卡方测试值? - How can I cross all variables against each other and gather Chi Square test values in R? 在 R 中创建排名变量的有效方法 - Efficient way to create rank variables in R 我如何在 R 中创建一个依赖于其他两个变量且必须具有特定值的变量? - How can i create a variable in R that is dependent of two other variables, and must have a certain value? 如何使用相互关联的变量模拟数据框? - How can I simulate a data frame with variables that correlate to each other? 如何在R的循环中创建具有不同y变量的图? - How can I create plots with different y variables in a loop in R? 问:我如何创建一个带有两个变量的直方图? - R Question: How can I create a histogram with 2 variables against eachother? 如何在 R 中将一些变量转换为数字,同时保持其他变量相同 - How can I convert some variables into numeric while keeping other variables same in R 在 R 中的 dplyr 中分组其他变量后,如何保留其他变量? - How can I keep additional variables after grouping in some other variables in dplyr in R? summarise() 中是否有 R function 可以计算两个相互重合的变量的数量? - Is there an R function in summarize() where I can count the amount of two variables coinciding with each other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM