不确定这是否正是你想要的,但
data.frame
下面这个对我有用。希望有帮助
df <- data.frame(Category=c(rep("Orange",10), rep("Banana",10)),
Score.08.2007=c(runif(6),rep(NA,4),runif(4),rep(NA,2),runif(4)),
Score.09.2017=c(runif(5),rep(NA,3),runif(2),runif(4),rep(NA,4),runif(2)),
stringsAsFactors=F)
dplyr解决方案
library(dplyr)
eq_ranks <- function(theseCols, newCols, df){
theseCols <- enquo(theseCols)
df1 <- df %>%
group_by(Category) %>%
mutate_at(vars(!!theseCols), funs(rank(., na.last="keep"))) %>%
ungroup() %>%
select(-Category) %>%
setNames(newCols)
df2 <- cbind(df, df1)
return(df2)
}
aux <- colnames(df)[-1]
newCols <- sub("Score", "Rank", aux)
eq_ranks(aux,newCols,df)
输出
structure(list(Category = c("Orange", "Orange", "Orange", "Orange",
"Orange", "Orange", "Orange", "Orange", "Orange", "Orange", "Banana",
"Banana", "Banana", "Banana", "Banana", "Banana", "Banana", "Banana",
"Banana", "Banana"), Score.08.2007 = c(0.757087148027495, 0.202692255144939,
0.711121222469956, 0.121691921027377, 0.245488513959572, 0.14330437942408,
NA, NA, NA, NA, 0.239629415096715, 0.0589343772735447, 0.642288258532062,
0.876269212691113, NA, NA, 0.778914677444845, 0.79730882588774,
0.455274453619495, 0.410084082046524), Score.09.2017 = c(0.810870242770761,
0.604933290276676, 0.654723928077146, 0.353197271935642, 0.270260145887733,
NA, NA, NA, 0.99268406117335, 0.633493264438584, 0.213208135217428,
0.129372348077595, 0.478118034312502, 0.924074469832703, NA,
NA, NA, NA, 0.59876096714288, 0.976170694921166), Rank.08.2007 = c(6,
3, 5, 1, 4, 2, NA, NA, NA, NA, 2, 1, 5, 8, NA, NA, 6, 7, 4, 3
), Rank.09.2017 = c(6, 3, 5, 2, 1, NA, NA, NA, 7, 4, 2, 1, 3,
5, NA, NA, NA, NA, 4, 6)), .Names = c("Category", "Score.08.2007",
"Score.09.2017", "Rank.08.2007", "Rank.09.2017"), row.names = c(NA,
-20L), class = "data.frame")