使用loop或apply命令计算R中的U统计信息

时间:2017-03-10 16:12:58

标签: r statistics

我正在考虑使用循环或应用命令来解决此问题,但失败了。 formula for reference

description for U statistics

1 个答案:

答案 0 :(得分:0)

如果使用data.table,可以借助CJ.dt函数(https://stackoverflow.com/a/27347397/5744762)找到解决方案。以下是我认为您所寻找的有限描述。

library(data.table)

#Create sample datasets
DT_tor <- data.table(ID = 1:100, time_tor = abs(rnorm(100)))
DT_bunny <- data.table(ID = 1:100, time_bunny = abs(rnorm(100)))

#CJ.dt function
CJ.dt = function(X,Y) {
  stopifnot(is.data.table(X),is.data.table(Y))
  k = NULL
  X = X[, c(k=1, .SD)]
  setkey(X, k)
  Y = Y[, c(k=1, .SD)]
  setkey(Y, NULL)
  X[Y, allow.cartesian=TRUE][, k := NULL][]
}

#Crossjoin two data.tables
DT_CJ <- CJ.dt(DT_tor, DT_bunny)

#Get a score for the tortoise and a score for the bunny
Score_tor <- DT_CJ[time_tor < time_bunny, .N]
Score_bunny <- DT_CJ[time_tor > time_bunny, .N]