我将 FUN = rank 传递给 ave,如何使 ties.methods = "first"
I'm passing FUN = rank to ave, how do I makes ties.methods = "first"
也许这是不可能的,但我想在 ave
.
中使用 rank 作为参数时指定一个平局方法
即。
df <- ave(df$subset, df$subset2, FUN = rank, ties.method = "first)
ave
似乎不接受其他排名参数。
我们需要在 ave
中使用匿名函数调用,因为 ave
不接受应该在其他地方工作的函数参数
with(df, ave(subset, subset2, FUN = function(x) rank(x, ties.method = "first)))
注意:注意到 ave
的输出分配给了 'df'。输出将是 vector
,如果我们需要数据集中的新列,则为 df$newCol <-
但是,data.table
或 dplyr
等包解决方案不需要此匿名调用
library(data.table)
setDT(df)[, newCol := frank(subset, ties.method = "first"), by = subset2]
也许这是不可能的,但我想在 ave
.
即。
df <- ave(df$subset, df$subset2, FUN = rank, ties.method = "first)
ave
似乎不接受其他排名参数。
我们需要在 ave
中使用匿名函数调用,因为 ave
不接受应该在其他地方工作的函数参数
with(df, ave(subset, subset2, FUN = function(x) rank(x, ties.method = "first)))
注意:注意到 ave
的输出分配给了 'df'。输出将是 vector
,如果我们需要数据集中的新列,则为 df$newCol <-
但是,data.table
或 dplyr
library(data.table)
setDT(df)[, newCol := frank(subset, ties.method = "first"), by = subset2]