2017-03-10 68 views
-1

我想使用循環或申請命令來解決這個問題,但未能ü統計。 formula for reference使用循環或應用命令來計算R中

description for U statistics

+1

'ifelse(A> B,1,0)'?此外,從一般意義上說明問題(您可以在Google搜索中找到答案),提供數據集並提供期望的輸出,這很有用。 –

+0

你到底做了什麼?它究竟如何失敗?這不是一個特定的編程問題。 – MrFlick

+0

@AndrewPruet;弗裏克先生,我附上了描述。很抱歉對於這個誤會。 –

回答

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] 
+0

非常感謝。但是如果我有野兔和烏龜的現有數據,那麼我該如何改變呢? –

+0

@SuxiZheng就像我問...此外,有資源的過多那裏弄清楚如何做你所問的你應該已經提供了這個問題的現有數據的樣本。你至少需要付出一些努力來解決你自己的問題。 –

+0

好的,我明白了!非常感謝! –