2016-09-20 104 views
1

我使用的是Rstudio(版本.99.903),有一臺PC(Windows 8)。隨着問題變得更加複雜,我從昨天開始跟進一個問題。下面是數據的樣子:將最近的平均值與當前值進行比較R

Number  Trial  ID Open date Enrollment rate 
420  NCT00091442 9 1/28/2005 0.2 
1476  NCT00301457 26 2/22/2008 1 
10559  NCT01307397 34 7/28/2011 0.6 
6794  NCT00948675 53 5/12/2010 0 
6451  NCT00917384 53 8/17/2010 0.3 
8754  NCT01168973 53 1/19/2011 0.2 
8578  NCT01140347 53 12/30/2011 2.4 
11655  NCT01358877 53 4/2/2012 0.3 
428  NCT00091442 55 9/7/2005 0.1 
112  NCT00065325 62 10/15/2003 0.2 
477  NCT00091442 62 11/11/2005 0.1 
16277  NCT01843374 62 12/16/2013 0.2 
17386  NCT01905657 62 1/8/2014 0.6 
411  NCT00091442 66 1/12/2005 0 

我需要做的是給定的ID內比較的最新日期的入學率平均那些長達一年之前,它是值的。例如,對於ID 53,2011年1月19日的日期註冊率爲0.2,我想將其與2010年8月17日和2010年5月12日的入學率(例如0.15)的平均值進行比較, 。

如果在當前ID之前的ID中沒有其他日期,則不應進行比較。例如,對於ID 26,就沒有比較。同樣,對於ID 53,5/12/2010將不會進行比較。

當我說「比較」時,我沒有做任何分析或可視化。我只想要一個新的列,這些列的入學率的平均值要比目前的一年的平均值高出一年(我將會密謀他們,並在百分位後面對他們進行排名)。有> 20,000個數據點。任何幫助將非常感激。

回答

-1

詳盡但可能是高性能的做法。沒有巨大的循環遍歷數據框的所有行。兩個sapply循環只能運行在一個大的數值向量上,不管你的數據行數是多少,它應該是相對較快的。但我相信很快就會有人用一個簡單的dplyr解決方案跳下舞。

方法假設您的數據首先按ID然後按Opendata排序。如果他們沒有排序,你需要先排序。

# Find indices where the same ID is above and below it 
A = which(unlist(sapply(X = rle(df$ID)$lengths, 
FUN = function(x) {if(x == 1) return(F) 
        if(x == 2) return(c(F,F)) 
        if(x >= 3) return(c(F,rep(T, x-2),F))}))) 

# Store list of date, should speed up code a tiny bit 
V_opendate = df$Opendate 
# Further filter on A, where the date difference < 365 days 
B = A[sapply(A, function(x) (abs(V_opendate[x]-V_opendate[x-1]) < 365) & (abs(V_opendate[x]-V_opendate[x+1]) < 365))] 

# Return actual indices of rows - 1, rows +1 
C = sapply(B, function(x) c(x-1, x+1), simplify = F) 

# Actually take the mean of these cases 
D = sapply(C, function(x) mean(df[x,]$Enrollment)) 

# Create new column rate and fill in with value of C. You can do the comparison from here. 
df[B,"Rate"] = D 

    Number  Trial ID Opendate Enrollmentrate Rate 
1  420 NCT00091442 9 2005-01-28   0.2 NA 
2 1476 NCT00301457 26 2008-02-22   1.0 NA 
3 10559 NCT01307397 34 2011-07-28   0.6 NA 
4 6794 NCT00948675 53 2010-05-12   0.0 NA 
5 6451 NCT00917384 53 2010-08-17   0.3 0.10 
6 8754 NCT01168973 53 2011-01-19   0.2 1.35 
7 8578 NCT01140347 53 2011-12-30   2.4 0.25 
8 11655 NCT01358877 53 2012-04-02   0.3 NA 
9  428 NCT00091442 55 2005-09-07   0.1 NA 
10 112 NCT00065325 62 2003-10-15   0.2 NA 
11 477 NCT00091442 62 2005-11-11   0.1 NA 
12 16277 NCT01843374 62 2013-12-16   0.2 NA 
13 17386 NCT01905657 62 2014-01-08   0.6 NA 
14 411 NCT00091442 66 2005-01-12   0.0 NA 
14 411 NCT00091442 66 1/12/2005   0.00 NA 

計算相關行。您可以與新創建的Rate列進行比較。

您可能需要更改代碼一點,因爲我改變了列名刪除空間

df = read.table(text = " Number  Trial  ID Opendate Enrollmentrate 
420  NCT00091442 9 1/28/2005 0.2 
       1476  NCT00301457 26 2/22/2008 1 
       10559  NCT01307397 34 7/28/2011 0.6 
       6794  NCT00948675 53 5/12/2010 0 
       6451  NCT00917384 53 8/17/2010 0.3 
       8754  NCT01168973 53 1/19/2011 0.2 
       8578  NCT01140347 53 12/30/2011 2.4 
       11655  NCT01358877 53 4/2/2012 0.3 
       428  NCT00091442 55 9/7/2005 0.1 
       112  NCT00065325 62 10/15/2003 0.2 
       477  NCT00091442 62 11/11/2005 0.1 
       16277  NCT01843374 62 12/16/2013 0.2 
       17386  NCT01905657 62 1/8/2014 0.6 
       411  NCT00091442 66 1/12/2005 0", header = T)