[英]Adding multiple ranking columns in a dataframe in R
I have the following df:我有以下 df:
id city uf home money work
34 LA RJ 10 2 2
33 BA TY 7 3 65
32 NY BN 4 5 4
12 SP SD 3 9 7
14 FR DE 1 8 9
17 BL DE 5 10 8
DESIRED DF:期望的 DF:
id city uf home rank_home money rank_money work rank_work
34 LA RJ 10 1 2 6 2 6
33 BA TY 7 2 3 5 65 1
32 NY BN 4 4 5 4 4 5
12 SP SD 3 5 9 2 7 4
14 FR DE 1 6 8 3 9 2
17 BL DE 5 3 10 1 8 3
I know this is possible: dat$rank_home <- rank(dat$home)
我知道这是可能的:
dat$rank_home <- rank(dat$home)
But I want a cleaner code for multiple columns!但我想要一个更清晰的多列代码!
Thank you!!谢谢!!
We can loop across
the columns 'home' to 'work', apply the rank
, while creating new column by adding prefix in .names
, and probably select
to keep the order我们可以循环
across
列“家”到“工作”,应用的rank
,而在添加前缀创建新列.names
,并可能select
维持秩序
library(dplyr)
df1 <- df %>%
mutate(across(home:work, ~ rank(-.), .names = "rank_{.col}"))
Or may do this in a loop where it is more flexible in placing the column at a particular position by specifying either .after
or .before
.或者可以在循环中执行此操作,其中它是在通过指定将所述柱在特定位置更加灵活
.after
或.before
。 Note that we used compound assignment operator ( %<>%
from magrittr
) to do the assignment in place请注意,我们使用复合赋值运算符(来自
magrittr
%<>%
)来进行就地赋值
library(magrittr)
library(stringr)
for(nm in names(df)[4:6]) df %<>%
mutate(!!str_c("rank_", nm) := rank(-.data[[nm]]), .after = all_of(nm))
-output -输出
df
id city uf home rank_home money rank_money work rank_work
1 34 LA RJ 10 1 2 6 2 6
2 33 BA TY 7 2 3 5 65 1
3 32 NY BN 4 4 5 4 4 5
4 12 SP SD 3 5 9 2 7 4
5 14 FR DE 1 6 8 3 9 2
6 17 BL DE 5 3 10 1 8 3
NOTE: If the column have ties, then the default method use is "average"
.注意:如果列有关系,则默认方法使用是
"average"
。 So, ties.method
can also be an argument in the rank
where there are ties.因此,
ties.method
也可以是存在关系的rank
中的参数。
df <- structure(list(id = c(34L, 33L, 32L, 12L, 14L, 17L), city = c("LA",
"BA", "NY", "SP", "FR", "BL"), uf = c("RJ", "TY", "BN", "SD",
"DE", "DE"), home = c(10L, 7L, 4L, 3L, 1L, 5L), money = c(2L,
3L, 5L, 9L, 8L, 10L), work = c(2L, 65L, 4L, 7L, 9L, 8L)),
class = "data.frame", row.names = c(NA,
-6L))
A base R option基本 R 选项
cbind(
df,
setNames(
lapply(-df[-(1:3)], rank),
paste0("rank_", names(df)[-(1:3)])
)
)
gives给
id city uf home money work rank_home rank_money rank_work
1 34 LA RJ 10 2 2 1 6 6
2 33 BA TY 7 3 65 2 5 1
3 32 NY BN 4 5 4 4 4 5
4 12 SP SD 3 9 7 5 2 4
5 14 FR DE 1 8 9 6 3 2
6 17 BL DE 5 10 8 3 1 3
One optional method in data.table
data.table
一种可选方法
library(data.table)
new <- paste0("rank_",names(dt[,4:6]))
dt[,(new):=lapply(-.SD,frankv),.SDcols=4:6]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.