2016-03-02 42 views
0

我想排名日期在一個date.frame,其中重複獲得相同的排名。 Furhter我想區分不同的類別。我沒有使用rank函數,因爲我得到了漂浮物。R等級data.frame在不同的類別

我的代碼正在工作,但非常緩慢。

我該如何讓這個更快?

for(i in 1:length(unique_IDS)){ 
temp_df=data.frame(rounds=unique(df$Dates[rounds$ID==unique_IDS[i]]), round_number=0) 
temp_df=temp_df[order(temp_df$rounds),] 
#temp_df$round_number=1:nrow(temp_df) 
df$round_number[df$ID==unique_IDS[i]]= match(x = df$Dates[df$ID==unique_IDS[i]],table=temp_df$rounds) 

}

問候

+2

儘量使你的問題重複性,並提供所需的輸出的一個例子。 –

回答

0

這是代碼

df=data.frame(x=runif(10, 1, 10), cat="A", stringsAsFactors = F) 
df$cat[3:5]="B" 
df$cat[6:10]="C" 

df$rank=0 
cats=unique(df$cat) 

for(i in 1:length(cats)){ 
    temp=df$x[df$cat==cats[i]]  
    temp=temp[order(temp)]  
    df$rank[df$cat==cats[i]]=match(df$x[df$cat==cats[i]], table = temp) 
} 

df 

最佳